1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
2 | // File : sdc_driver.c |
---|
3 | // Date : 31/08/2012 |
---|
4 | // Author : cesar fuguet |
---|
5 | // Copyright (c) UPMC-LIP6 |
---|
6 | /////////////////////////////////////////////////////////////////////////////////// |
---|
7 | |
---|
8 | // #include <hard_config.h> |
---|
9 | #include <soclib_sdc.h> |
---|
10 | //#include <tty0.h> |
---|
11 | //#include <utils.h> |
---|
12 | #include <hal_kernel_types.h> |
---|
13 | #include <chdev.h> |
---|
14 | #include <dev_ioc.h> |
---|
15 | #include <thread.h> |
---|
16 | #include <printk.h> |
---|
17 | |
---|
18 | #define SYSCLK_FREQ (RESET_SYSTEM_CLK * 1000) |
---|
19 | #define SDCARD_RESET_ITER_MAX 4 |
---|
20 | |
---|
21 | extern chdev_directory_t chdev_dir; // allocated in the kernel_init.c file. |
---|
22 | |
---|
23 | /////////////////////////////////////////////////////////////////////////////// |
---|
24 | // Global variables |
---|
25 | /////////////////////////////////////////////////////////////////////////////// |
---|
26 | |
---|
27 | __attribute__((section(".kdata"))) |
---|
28 | static struct sdcard_dev global_sdcard; |
---|
29 | |
---|
30 | __attribute__((section(".kdata"))) |
---|
31 | static struct spi_dev* spi; |
---|
32 | |
---|
33 | /////////////////////////////////////////////////////////////////////////////// |
---|
34 | // This function enables SD Card select signal |
---|
35 | /////////////////////////////////////////////////////////////////////////////// |
---|
36 | static void _sdc_enable(sdcard_dev_t *sdcard) |
---|
37 | { |
---|
38 | spi_ss_assert(sdcard->spi, sdcard->slave_id); |
---|
39 | } |
---|
40 | |
---|
41 | /////////////////////////////////////////////////////////////////////////////// |
---|
42 | // This function disables SD Card select signal |
---|
43 | /////////////////////////////////////////////////////////////////////////////// |
---|
44 | static void _sdc_disable(sdcard_dev_t *sdcard) |
---|
45 | { |
---|
46 | spi_ss_deassert(sdcard->spi, sdcard->slave_id); |
---|
47 | } |
---|
48 | |
---|
49 | /////////////////////////////////////////////////////////////////////////////// |
---|
50 | // This function writes on the SPI tx register to generate SD card clock ticks |
---|
51 | // - tick_count: number of ticks to generate (1 tick -> 8 clocks) |
---|
52 | /////////////////////////////////////////////////////////////////////////////// |
---|
53 | static void _sdc_gen_tick(sdcard_dev_t *sdcard, unsigned int tick_count) |
---|
54 | { |
---|
55 | register int i = 0; |
---|
56 | while(i++ < tick_count) spi_put_tx(sdcard->spi, 0xFF, 0); |
---|
57 | } |
---|
58 | |
---|
59 | /////////////////////////////////////////////////////////////////////////////// |
---|
60 | // This function changes the SD card access pointer position in terms of |
---|
61 | // blocks |
---|
62 | // - lba: number of logical block to move the pointer |
---|
63 | /////////////////////////////////////////////////////////////////////////////// |
---|
64 | void _sdc_lseek(sdcard_dev_t *sdcard, unsigned int lba) |
---|
65 | { |
---|
66 | sdcard->access_pointer = sdcard->block_length * lba; |
---|
67 | } |
---|
68 | |
---|
69 | /////////////////////////////////////////////////////////////////////////////// |
---|
70 | // This function gets a byte from the SD card |
---|
71 | /////////////////////////////////////////////////////////////////////////////// |
---|
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); |
---|
77 | } |
---|
78 | |
---|
79 | /////////////////////////////////////////////////////////////////////////////// |
---|
80 | // This function returns when a valid response from the SD card is received or |
---|
81 | // a timeout has been triggered |
---|
82 | // Returns the SD card response value |
---|
83 | /////////////////////////////////////////////////////////////////////////////// |
---|
84 | static unsigned char _sdc_wait_response(sdcard_dev_t *sdcard) |
---|
85 | { |
---|
86 | unsigned char sdcard_rsp; |
---|
87 | register int iter; |
---|
88 | |
---|
89 | iter = 0; |
---|
90 | sdcard_rsp = _sdc_receive_char(sdcard); |
---|
91 | while ( |
---|
92 | (iter < SDCARD_COMMAND_TIMEOUT) && |
---|
93 | !SDCARD_CHECK_R1_VALID(sdcard_rsp) |
---|
94 | ) |
---|
95 | { |
---|
96 | sdcard_rsp = _sdc_receive_char(sdcard); |
---|
97 | iter++; |
---|
98 | } |
---|
99 | |
---|
100 | return sdcard_rsp; |
---|
101 | } |
---|
102 | |
---|
103 | /////////////////////////////////////////////////////////////////////////////// |
---|
104 | // This function returns when a data block from the SD card is received (data |
---|
105 | // block start marker received). |
---|
106 | // It must be called after a read command |
---|
107 | /////////////////////////////////////////////////////////////////////////////// |
---|
108 | static void _sdc_wait_data_block(sdcard_dev_t *sdcard) |
---|
109 | { |
---|
110 | while (_sdc_receive_char(sdcard) != 0xFE); |
---|
111 | } |
---|
112 | |
---|
113 | /////////////////////////////////////////////////////////////////////////////// |
---|
114 | // This function sends a command to the SD card |
---|
115 | // - index: CMD index |
---|
116 | // - app: type of command |
---|
117 | // - args: CMD arguments vector |
---|
118 | // - crc7: CRC (7 bits) to send |
---|
119 | /////////////////////////////////////////////////////////////////////////////// |
---|
120 | static int _sdc_send_command ( sdcard_dev_t *sdcard, |
---|
121 | int index, |
---|
122 | int app , |
---|
123 | void * args , |
---|
124 | unsigned crc7 ) |
---|
125 | { |
---|
126 | unsigned char sdcard_rsp; |
---|
127 | unsigned char * _args; |
---|
128 | |
---|
129 | _sdc_gen_tick(sdcard, 5); |
---|
130 | |
---|
131 | if (app == SDCARD_ACMD) |
---|
132 | { |
---|
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); |
---|
141 | if (SDCARD_CHECK_R1_ERROR(sdcard_rsp)) |
---|
142 | { |
---|
143 | return sdcard_rsp; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | _args = (unsigned char *) args; |
---|
148 | |
---|
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); |
---|
159 | } |
---|
160 | |
---|
161 | /////////////////////////////////////////////////////////////////////////////// |
---|
162 | // This function initializes the SD card (reset procedure) |
---|
163 | // - channel: channel index (only channel 0 is supported) |
---|
164 | // Returns 0 if success, other value if failure |
---|
165 | /////////////////////////////////////////////////////////////////////////////// |
---|
166 | static int _sdc_open( sdcard_dev_t *sdcard, xptr_t base, unsigned int channel ) |
---|
167 | { |
---|
168 | unsigned char args[4]; |
---|
169 | unsigned char sdcard_rsp; |
---|
170 | unsigned int iter, ersp; |
---|
171 | |
---|
172 | sdcard->spi = base; |
---|
173 | sdcard->slave_id = channel; |
---|
174 | |
---|
175 | // supply SD card ramp up time (min 74 cycles) |
---|
176 | _sdc_gen_tick(sdcard, 10); |
---|
177 | |
---|
178 | // Assert slave select signal |
---|
179 | // Send CMD0 (Reset Command) |
---|
180 | // Deassert slave select signal |
---|
181 | _sdc_enable(sdcard); |
---|
182 | |
---|
183 | args[0] = 0; |
---|
184 | args[1] = 0; |
---|
185 | args[2] = 0; |
---|
186 | args[3] = 0; |
---|
187 | sdcard_rsp = _sdc_send_command(sdcard, 0, SDCARD_CMD, args, 0x4A); |
---|
188 | |
---|
189 | if ( sdcard_rsp != 0x01 ) |
---|
190 | { |
---|
191 | printk("[SDC ERROR] card CMD0 failed\n"); |
---|
192 | return sdcard_rsp; |
---|
193 | } |
---|
194 | |
---|
195 | _sdc_disable(sdcard); |
---|
196 | |
---|
197 | // send CMD8. If card is pre-v2, It will reply with illegal command. |
---|
198 | // Otherwise we announce sdhc support. |
---|
199 | _sdc_enable(sdcard); |
---|
200 | args[0] = 0; |
---|
201 | args[1] = 0; |
---|
202 | args[2] = 0x01; |
---|
203 | args[3] = 0x01; |
---|
204 | sdcard_rsp = _sdc_send_command(sdcard, 8, SDCARD_CMD, args, 0x63); |
---|
205 | if (!SDCARD_CHECK_R1_VALID(sdcard_rsp)) |
---|
206 | { |
---|
207 | printk("[SDC ERROR] card CMD8 failed\n"); |
---|
208 | return sdcard_rsp; |
---|
209 | } |
---|
210 | if (!SDCARD_CHECK_R1_ERROR(sdcard_rsp)) |
---|
211 | { |
---|
212 | // no error, command accepted. get whole reply |
---|
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); |
---|
217 | if ((ersp & 0xffff) != 0x0101) |
---|
218 | { |
---|
219 | // voltage mismatch |
---|
220 | printk("[SDC ERROR] card CMD8 mismatch : ersp = %x\n"); |
---|
221 | return sdcard_rsp; |
---|
222 | } |
---|
223 | printk("[SDC WARNING] v2 or later "); |
---|
224 | sdcard->sdhc = 1; |
---|
225 | } |
---|
226 | else if ((sdcard_rsp & SDCARD_R1_ILLEGAL_CMD) == 0) |
---|
227 | { |
---|
228 | // other error |
---|
229 | printk("[SDC ERROR] card CMD8 error\n"); |
---|
230 | return sdcard_rsp; |
---|
231 | } |
---|
232 | else |
---|
233 | { |
---|
234 | sdcard->sdhc = 0; |
---|
235 | } |
---|
236 | _sdc_disable(sdcard); |
---|
237 | |
---|
238 | // send CMD41, enabling the card |
---|
239 | _sdc_enable(sdcard); |
---|
240 | args[0] = sdcard->sdhc ? 0x40: 0; |
---|
241 | args[1] = 0; |
---|
242 | args[2] = 0; |
---|
243 | args[3] = 0; |
---|
244 | |
---|
245 | iter = 0; |
---|
246 | while( iter++ < SDCARD_COMMAND_TIMEOUT ) |
---|
247 | { |
---|
248 | sdcard_rsp = _sdc_send_command(sdcard, 41, SDCARD_ACMD, args, 0x00); |
---|
249 | if( sdcard_rsp == 0x01 ) |
---|
250 | { |
---|
251 | continue; |
---|
252 | } |
---|
253 | |
---|
254 | break; |
---|
255 | } |
---|
256 | |
---|
257 | _sdc_disable(sdcard); |
---|
258 | if (sdcard_rsp) |
---|
259 | { |
---|
260 | printk("[SDC ERROR] ACMD41 failed\n"); |
---|
261 | return sdcard_rsp; |
---|
262 | } |
---|
263 | if (sdcard->sdhc != 0) |
---|
264 | { |
---|
265 | // get the card capacity to see if it's really HC |
---|
266 | _sdc_enable(sdcard); |
---|
267 | args[0] = sdcard->sdhc ? 0x40: 0; |
---|
268 | args[1] = 0; |
---|
269 | args[2] = 0; |
---|
270 | args[3] = 0; |
---|
271 | sdcard_rsp = _sdc_send_command(sdcard, 58, SDCARD_CMD, args, 0x00); |
---|
272 | if (sdcard_rsp) |
---|
273 | { |
---|
274 | printk("[SDC ERROR] CMD58 failed\n"); |
---|
275 | return sdcard_rsp; |
---|
276 | } |
---|
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); |
---|
281 | if (ersp & 0x40000000) |
---|
282 | { |
---|
283 | printk(" SDHC "); |
---|
284 | } |
---|
285 | else |
---|
286 | { |
---|
287 | sdcard->sdhc = 0; |
---|
288 | } |
---|
289 | _sdc_disable(sdcard); |
---|
290 | } |
---|
291 | printk("card detected\n"); |
---|
292 | return 0; |
---|
293 | } |
---|
294 | |
---|
295 | /////////////////////////////////////////////////////////////////////////////// |
---|
296 | // This function sets the block size in the SD card. |
---|
297 | // - len: block size in bytes (only 512 bytes supported) |
---|
298 | // Returns 0 if success, other value if failure |
---|
299 | /////////////////////////////////////////////////////////////////////////////// |
---|
300 | static unsigned int _sdc_set_block_size(sdcard_dev_t *sdcard, unsigned int len) |
---|
301 | { |
---|
302 | unsigned char args[4]; |
---|
303 | unsigned char sdcard_rsp; |
---|
304 | register int i; |
---|
305 | |
---|
306 | // For now, supported block size is 512 bytes |
---|
307 | if (len != 512) return 1; |
---|
308 | |
---|
309 | // When using high capacity SDCARD, the block_length is not a number of bytes |
---|
310 | // but a number of blocks (transfer unit) |
---|
311 | if (sdcard->sdhc) |
---|
312 | { |
---|
313 | sdcard->block_length = len / 512; |
---|
314 | return 0; |
---|
315 | } |
---|
316 | |
---|
317 | for (i = 0; i < 4; i++) |
---|
318 | { |
---|
319 | args[i] = (len >> (32 - (i+1)*8)) & 0xFF; |
---|
320 | } |
---|
321 | |
---|
322 | _sdc_enable(sdcard); |
---|
323 | |
---|
324 | sdcard_rsp = _sdc_send_command(sdcard, 16, SDCARD_CMD, args, 0x00); |
---|
325 | if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) ) |
---|
326 | { |
---|
327 | _sdc_disable(sdcard); |
---|
328 | return sdcard_rsp; |
---|
329 | } |
---|
330 | |
---|
331 | _sdc_disable(sdcard); |
---|
332 | |
---|
333 | sdcard->block_length = len; |
---|
334 | |
---|
335 | return 0; |
---|
336 | } |
---|
337 | |
---|
338 | ///////////////////////////////////////////////////////////////////////////////// |
---|
339 | // Extern functions |
---|
340 | ///////////////////////////////////////////////////////////////////////////////// |
---|
341 | |
---|
342 | //////////////////////// |
---|
343 | void soclib_sdc_init( chdev_t* chdev ) |
---|
344 | { |
---|
345 | // initializing the SPI controller |
---|
346 | _spi_init ( |
---|
347 | chdev->base , |
---|
348 | 200000 , /**< SPI_clk: 200 Khz */ |
---|
349 | SYSCLK_FREQ , /**< Sys_clk */ |
---|
350 | 8 , /**< Charlen: 8 */ |
---|
351 | SPI_TX_NEGEDGE, |
---|
352 | SPI_RX_POSEDGE |
---|
353 | ); |
---|
354 | |
---|
355 | // initializing the SD Card |
---|
356 | unsigned int iter = 0; |
---|
357 | unsigned char sdcard_rsp; |
---|
358 | unsigned int i; |
---|
359 | |
---|
360 | while(1) |
---|
361 | { |
---|
362 | printk("[SDC WARNING] Trying to initialize SD card...\n"); |
---|
363 | |
---|
364 | sdcard_rsp = _sdc_open(&global_sdcard, chdev->base, 0 ); // only channel 0 |
---|
365 | if (sdcard_rsp == 0) |
---|
366 | { |
---|
367 | printk("OK\n"); |
---|
368 | break; |
---|
369 | } |
---|
370 | |
---|
371 | printk("KO\n"); |
---|
372 | |
---|
373 | for (i = 0; i < 1000; i++); |
---|
374 | |
---|
375 | if (++iter >= SDCARD_RESET_ITER_MAX) |
---|
376 | { |
---|
377 | assert( false, __FUNCTION__, "\n[SDC ERROR] During SD card reset / card response = %x \n", sdcard_rsp); |
---|
378 | } |
---|
379 | } |
---|
380 | |
---|
381 | // set the block length of the SD Card |
---|
382 | sdcard_rsp = _sdc_set_block_size(&global_sdcard, 512); |
---|
383 | if (sdcard_rsp) |
---|
384 | { |
---|
385 | assert( false, __FUNCTION__, "[SDC ERROR] During SD card block size initialization\n"); |
---|
386 | } |
---|
387 | |
---|
388 | // incrementing SDCARD clock frequency for normal function |
---|
389 | _spi_init ( |
---|
390 | chdev->base , |
---|
391 | 10000000 , // SPI_clk 10 Mhz |
---|
392 | SYSCLK_FREQ , // Sys_clk |
---|
393 | -1 , // Charlen: 8 |
---|
394 | -1 , |
---|
395 | -1 |
---|
396 | ); |
---|
397 | |
---|
398 | printk("[SDC WARNING] Finish SD card initialization\n\r"); |
---|
399 | |
---|
400 | /* Initialize the chdev */ |
---|
401 | // get extended pointer on SOCLIB_BDV peripheral base address |
---|
402 | xptr_t sdc_xp = chdev->base; |
---|
403 | |
---|
404 | // set driver specific fields |
---|
405 | chdev->cmd = &soclib_sdc_cmd; |
---|
406 | chdev->isr = &soclib_sdc_isr; |
---|
407 | |
---|
408 | } // end soclib_sdc_init() |
---|
409 | |
---|
410 | ///////////////////////////////////////////////////// |
---|
411 | void __attribute__ ((noinline)) soclib_sdc_cmd( xptr_t th_xp ) |
---|
412 | { |
---|
413 | cxy_t th_cxy = GET_CXY( th_xp ); |
---|
414 | thread_t * th_ptr = GET_PTR( th_xp ); |
---|
415 | |
---|
416 | // get command arguments and extended pointer on IOC device |
---|
417 | uint32_t cmd_type = hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.type ) ); |
---|
418 | unsigned int lba = hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.lba ) ); |
---|
419 | xptr_t buf_xp = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->ioc_cmd.buf_xp ) ); |
---|
420 | unsigned int count = hal_remote_lw ( XPTR( th_cxy , &th_ptr->ioc_cmd.count ) ); |
---|
421 | |
---|
422 | unsigned char args[4]; |
---|
423 | unsigned char sdcard_rsp; |
---|
424 | unsigned int i; |
---|
425 | unsigned int curr = lba; |
---|
426 | unsigned int last = lba + count; |
---|
427 | |
---|
428 | if ( cmd_type == IOC_SYNC_READ ) // read access |
---|
429 | { |
---|
430 | for ( ; curr < last ; curr++ ) |
---|
431 | { |
---|
432 | _sdc_lseek(&global_sdcard, curr); |
---|
433 | |
---|
434 | for (i = 0; i < 4; i++) |
---|
435 | { |
---|
436 | args[i] = (global_sdcard.access_pointer >> (32 - (i+1)*8)) & 0xFF; |
---|
437 | } |
---|
438 | |
---|
439 | _sdc_enable(&global_sdcard); |
---|
440 | |
---|
441 | sdcard_rsp = _sdc_send_command(&global_sdcard, 17, SDCARD_CMD, args, 0x00); |
---|
442 | if ( SDCARD_CHECK_R1_ERROR(sdcard_rsp) ) |
---|
443 | { |
---|
444 | _sdc_disable(&global_sdcard); |
---|
445 | return sdcard_rsp; |
---|
446 | } |
---|
447 | |
---|
448 | _sdc_wait_data_block(&global_sdcard); |
---|
449 | |
---|
450 | if (spi_get_data(global_sdcard.spi, buf_xp, 512 )) |
---|
451 | { |
---|
452 | _sdc_disable(&global_sdcard); |
---|
453 | return 1; |
---|
454 | } |
---|
455 | |
---|
456 | // Get the CRC16 (comes at the end of the data block) |
---|
457 | _sdc_receive_char(&global_sdcard); // first byte |
---|
458 | _sdc_receive_char(&global_sdcard); // second byte |
---|
459 | |
---|
460 | _sdc_disable(&global_sdcard); |
---|
461 | |
---|
462 | buf_xp += 512; |
---|
463 | } |
---|
464 | } |
---|
465 | else // write access |
---|
466 | { |
---|
467 | assert( false, __FUNCTION__, "[SDC ERROR] function _sdc_write() not iplemented yet\n"); |
---|
468 | } |
---|
469 | } // _end sdc_access() |
---|
470 | |
---|
471 | /////////////////////////////////////////////////////////////////////////////// |
---|
472 | // This ISR handles the IRQ generated by a SDC controler |
---|
473 | /////////////////////////////////////////////////////////////////////////////// |
---|
474 | void __attribute__ ((noinline)) soclib_sdc_isr( chdev_t * chdev ) |
---|
475 | { |
---|
476 | assert( false, __FUNCTION__, "\n[GIET ERROR] _sdc_isr() not implemented\n"); |
---|
477 | } |
---|
478 | |
---|
479 | // Local Variables: |
---|
480 | // tab-width: 4 |
---|
481 | // c-basic-offset: 4 |
---|
482 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
483 | // indent-tabs-mode: nil |
---|
484 | // End: |
---|
485 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|