summaryrefslogtreecommitdiff
path: root/firmware/target/arm/rk27xx/sd-rk27xx.c
blob: cb5c02e0b6a80f365fba24131a6b483665cff9eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 Daniel Ankers
 * Copyright © 2008-2009 Rafaël Carré
 * Copyright (C) 2011 Marcin Bukat
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "config.h" /* for HAVE_MULTIVOLUME */
#include "fat.h"
#include "thread.h"
#include "gcc_extensions.h"
#include "led.h"
#include "sdmmc.h"
#include "system.h"
#include "kernel.h"
#include "cpu.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "panic.h"
#include "stdbool.h"
#include "ata_idle_notify.h"
#include "sd.h"
#include "usb.h"

#ifdef HAVE_HOTSWAP
#include "disk.h"
#endif

#include "lcd.h"
#include <stdarg.h>
#include "sysfont.h"

#define RES_NO (-1)

/* debug stuff */
unsigned long sd_debug_time_rd = 0;
unsigned long sd_debug_time_wr = 0;

static tCardInfo card_info;

/* for compatibility */
static long last_disk_activity = -1;

static long sd_stack [(DEFAULT_STACK_SIZE*2 + 0x200)/sizeof(long)];
static const char         sd_thread_name[] = "ata/sd";
static struct mutex       sd_mtx SHAREDBSS_ATTR;
static struct event_queue sd_queue;
#ifndef BOOTLOADER
bool sd_enabled = false;
#endif

static struct semaphore transfer_completion_signal;
static struct semaphore command_completion_signal;
static volatile bool retry;
static volatile int cmd_error;

/* interrupt handler for SD */
void INT_SD(void)
{
    const int status = SD_INT;

    SD_INT = 0; /* disable sd interrupts, clear pending interrupts */

    /* cmd and response status pending */
    if(status & CMD_RES_STAT)
    {
        /* get the status */
        cmd_error = SD_CMDRES;
        semaphore_release(&command_completion_signal);
    }    

    /* data transfer status pending */
    if(status & DATA_XFER_STAT)
    {
        cmd_error = SD_DATAT;
        if (cmd_error & DATA_XFER_ERR)
            retry = true;

        semaphore_release(&transfer_completion_signal);
    }

    SD_INT = CMD_RES_INT_EN | DATA_XFER_INT_EN;
}

/* Exchange buffers - the one where SD module puts into/reads from
 * data and the one controlled by MCU. This allows some overlap
 * in transfer operations and should increase throuput.
 */
static void mmu_switch_buff(void)
{
    static unsigned int i = 0;

    if (i++ & 0x01)
    {
        MMU_CTRL = MMU_MMU0_BUFII | MMU_CPU_BUFI | MMU_BUFII_RESET |
                   MMU_BUFII_BYTE | MMU_BUFI_RESET | MMU_BUFI_WORD;
    }
    else
    {
        MMU_CTRL = MMU_MMU0_BUFI | MMU_CPU_BUFII | MMU_BUFII_RESET |
                   MMU_BUFII_WORD | MMU_BUFI_RESET | MMU_BUFI_BYTE;
    }
}

/* Reset internal pointers of the MMU submodule */
static void mmu_buff_reset(void)
{
    MMU_CTRL |= MMU_BUFII_RESET | MMU_BUFI_RESET;
}

static inline bool card_detect_target(void)
{
#if defined(RK27_GENERIC)
/* My generic device uses PC7 pin, active low */
    return !(GPIO_PCDR & 0x80);
#elif defined(HM60X) || defined(HM801)
    return !(GPIO_PFDR & (1<<2));
#else
#error "Unknown target"
#endif
}

/* Send command to the SD card. Command finish is signaled in ISR */
static bool send_cmd(const int cmd, const int arg, const int res,
        unsigned long *response)
{
    SD_CMD = arg;

    if (res > 0)
       SD_CMDREST = CMD_XFER_START | RES_XFER_START | res | cmd;
    else
       SD_CMDREST = CMD_XFER_START | RES_XFER_END | RES_R1 | cmd;

    semaphore_wait(&command_completion_signal, TIMEOUT_BLOCK);

    /*  Handle command responses & errors */
    if(res != RES_NO)
    {
        if(cmd_error & STAT_CMD_RES_ERR)
            return false;

        if(res == RES_R2)
        {
            response[0] = SD_RES3;
            response[1] = SD_RES2;
            response[2] = SD_RES1;
            response[3] = SD_RES0;
        }
        else
            response[0] = SD_RES3;
    }
    return true;
}

#if 0
/* for some misterious reason the card does not report itself as being in TRAN
 * but transfers are successful. Rockchip OF does not check the card state
 * after SELECT. I checked two different cards. 
 */
static void print_card_status(void)
{
    unsigned long response;
    send_cmd(SD_SEND_STATUS, card_info.rca, RES_R1,
             &response);

    printf("card status: 0x%0x, state: 0x%0x", response, (response>>9)&0xf);
}

static int sd_wait_for_tran_state(void)
{
    unsigned long response;
    unsigned int timeout = current_tick + 5*HZ;
    int cmd_retry = 10;

    while (1)
    {
        while (!send_cmd(SD_SEND_STATUS, card_info.rca, RES_R1,
                         &response) && cmd_retry > 0)
        {
            cmd_retry--;
        }

        if (cmd_retry <= 0)
        {
            return -1;
        }

        if (((response >> 9) & 0xf) == SD_TRAN)
        {
            return 0;
        }
    
        if(TIME_AFTER(current_tick, timeout))
        {
            return -10 * ((response >> 9) & 0xf);
        }

        last_disk_activity = current_tick;
    }
}
#endif

static bool sd_wait_card_busy(void)
{
    unsigned int timeout = current_tick + 5*HZ;

    while (!(SD_CARD & SD_CARD_BSY))
    {
        if(TIME_AFTER(current_tick, timeout))
            return false;
    }

    return true;
}

static int sd_init_card(void)
{
    unsigned long response;
    long init_timeout;
    bool sd_v2 = false;

    card_info.rca = 0;

    /*  assume 50 MHz APB freq / 125 = 400 kHz  */
    SD_CTRL = (SD_CTRL & ~(0x7FF)) | 0x7D;

    /* 100 - 400kHz clock required for Identification Mode  */
    /*  Start of Card Identification Mode ************************************/

    /* CMD0 Go Idle  */
    if(!send_cmd(SD_GO_IDLE_STATE, 0, RES_NO, NULL))
        return -1;
    
    sleep(1);

    /* CMD8 Check for v2 sd card.  Must be sent before using ACMD41
      Non v2 cards will not respond to this command*/
    if(send_cmd(SD_SEND_IF_COND, 0x1AA, RES_R6, &response))
        if((response & 0xFFF) == 0x1AA)
            sd_v2 = true;

    /* timeout for initialization is 1sec, from SD Specification 2.00 */
    init_timeout = current_tick + HZ;

    do {
        /* this timeout is the only valid error for this loop*/
        if(TIME_AFTER(current_tick, init_timeout))
            return -2;

        if(!send_cmd(SD_APP_CMD, card_info.rca, RES_R1, &response))
            return -3;

        sleep(1); /* bus conflict otherwise */

         /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
        if(!send_cmd(SD_APP_OP_COND, (0x00FF8000 | (sd_v2 ? 1<<30 : 0)),
                     RES_R3, &card_info.ocr))
            return -4;
    } while(!(card_info.ocr & (1<<31)) );

    /* CMD2 send CID */
    if(!send_cmd(SD_ALL_SEND_CID, 0, RES_R2, card_info.cid))
        return -5;

    /* CMD3 send RCA */
    if(!send_cmd(SD_SEND_RELATIVE_ADDR, 0, RES_R6, &card_info.rca))
        return -6;

    /*  End of Card Identification Mode   ************************************/


    /* CMD9 send CSD */
    if(!send_cmd(SD_SEND_CSD, card_info.rca, RES_R2, card_info.csd))
        return -11;

    sd_parse_csd(&card_info);

    if(!send_cmd(SD_SELECT_CARD, card_info.rca, RES_R1b, &response))
        return -20;

    if (!sd_wait_card_busy())
        return -21;

    /* CMD6 */
    if(!send_cmd(SD_SWITCH_FUNC, 0x80fffff1, RES_R1, &response))
        return -8;
    sleep(HZ/10);

    /*  Card back to full speed  25MHz*/
    SD_CTRL = (SD_CTRL & ~0x7FF);
    card_info.initialized = 1;

    return 0;
}

static void sd_thread(void) NORETURN_ATTR;
static void sd_thread(void)
{
    struct queue_event ev;
    bool idle_notified = false;

    while (1)
    {
        queue_wait_w_tmo(&sd_queue, &ev, HZ);

        switch ( ev.id )
        {
#ifdef HAVE_HOTSWAP
        case SYS_HOTSWAP_INSERTED:
        case SYS_HOTSWAP_EXTRACTED:
        {
            int microsd_init = 1;
            fat_lock();          /* lock-out FAT activity first -
                                    prevent deadlocking via disk_mount that
                                    would cause a reverse-order attempt with
                                    another thread */
            mutex_lock(&sd_mtx); /* lock-out card activity - direct calls
                                    into driver that bypass the fat cache */

            /* We now have exclusive control of fat cache and ata */

            disk_unmount(sd_first_drive);     /* release "by force", ensure file
                                    descriptors aren't leaked and any busy
                                    ones are invalid if mounting */
            /* Force card init for new card, re-init for re-inserted one or
             * clear if the last attempt to init failed with an error. */
            card_info.initialized = 0;

            if (ev.id == SYS_HOTSWAP_INSERTED)
            {
                sd_enable(true);
                microsd_init = sd_init_card(sd_first_drive);
                if (microsd_init < 0) /* initialisation failed */
                    panicf("microSD init failed : %d", microsd_init);

                microsd_init = disk_mount(sd_first_drive); /* 0 if fail */
            }

            /*
             * Mount succeeded, or this was an EXTRACTED event,
             * in both cases notify the system about the changed filesystems
             */
            if (microsd_init)
                queue_broadcast(SYS_FS_CHANGED, 0);

            sd_enable(false);

            /* Access is now safe */
            mutex_unlock(&sd_mtx);
            fat_unlock();
            }
            break;
#endif
        case SYS_TIMEOUT:
            if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
            {
                idle_notified = false;
            }
            else if (!idle_notified)
            {
                call_storage_idle_notifys(false);
                idle_notified = true;
            }
            break;

        case SYS_USB_CONNECTED:
            usb_acknowledge(SYS_USB_CONNECTED_ACK);
            /* Wait until the USB cable is extracted again */
            usb_wait_for_disconnect(&sd_queue);

            break;
        }
    }
}

static void init_controller(void)
{
    /* reset SD module */
    SCU_RSTCFG |= RSTCFG_SD;
    sleep(1);
    SCU_RSTCFG &= ~RSTCFG_SD;

    /* set pins functions as SD signals */
    SCU_IOMUXA_CON |= IOMUX_SD;

    /* enable and unmask SD interrupts in interrupt controller */
    SCU_CLKCFG &= ~CLKCFG_SD;
    INTC_IMR |= (1<<10);
    INTC_IECR |= (1<<10);

    SD_CTRL = SD_PWR_CPU | SD_DETECT_MECH | SD_CLOCK_EN | 0x7D;
    SD_INT = CMD_RES_INT_EN | DATA_XFER_INT_EN;
    SD_CARD = SD_CARD_SELECT | SD_CARD_PWR_EN;

    /* setup mmu buffers */
    MMU_PNRI = 0x1ff;
    MMU_PNRII = 0x1ff;
    MMU_CTRL = MMU_MMU0_BUFII | MMU_CPU_BUFI | MMU_BUFII_RESET |
               MMU_BUFII_BYTE | MMU_BUFI_RESET | MMU_BUFI_WORD;

    /* setup A2A DMA CH0 for SD reads */
    A2A_ISRC0 = (unsigned long)(&MMU_DATA);
    A2A_ICNT0 = 512;
    A2A_LCNT0 = 1;

    /* setup A2A DMA CH1 for SD writes */
    A2A_IDST1 = (unsigned long)(&MMU_DATA);
    A2A_ICNT1 = 512;
    A2A_LCNT1 = 1;

    /* src and dst for CH0 and CH1 is AHB0 */
    A2A_DOMAIN = 0;

#ifdef RK27XX_SD_DEBUG
    /* setup Timer1 for profiling purposes */
    TMR1CON = (1<<8)|(1<<3);
#endif
}

int sd_init(void)
{
    int ret;

    semaphore_init(&transfer_completion_signal, 1, 0);
    semaphore_init(&command_completion_signal, 1, 0);

    init_controller();

    ret = sd_init_card();
    if(ret < 0)
        return ret;

    /* init mutex */
    mutex_init(&sd_mtx);

    queue_init(&sd_queue, true);
    create_thread(sd_thread, sd_stack, sizeof(sd_stack), 0,
            sd_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU));

    return 0;
}

static inline void read_sd_data(unsigned char **dst)
{
    commit_discard_dcache_range((const void *)*dst, 512);

    A2A_IDST0 = (unsigned long)*dst;
    A2A_CON0  = (3<<9) |    /* burst 16 */
                (1<<6) |    /* fixed src */
                (1<<3) |    /* DMA start */
                (2<<1) |    /* word transfer size */
                (1<<0);     /* software mode */

    /* wait for DMA engine to finish transfer */
    while (A2A_DMA_STS & 1);

    *dst += 512;
}

static inline void write_sd_data(unsigned char **src)
{
    commit_discard_dcache_range((const void *)*src, 512);

    A2A_ISRC1 = (unsigned long)*src;
    A2A_CON1  = (3<<9) |    /* burst 16 */
                (1<<5) |    /* fixed dst */
                (1<<3) |    /* DMA start */
                (2<<1) |    /* word transfer size */
                (1<<0);     /* software mode */

    /* wait for DMA engine to finish transfer */
    while (A2A_DMA_STS & 2);

    *src += 512;
}

int sd_read_sectors(IF_MD2(int drive,) unsigned long start, int count,
                    void* buf)
{
#ifdef HAVE_MULTIDRIVE
    (void)drive;
#endif
    unsigned long response;
    unsigned int retry_cnt = 0;
    int cnt, ret = 0;
    unsigned char *dst;

    mutex_lock(&sd_mtx);
    sd_enable(true);

    if (count <= 0 || start + count > card_info.numblocks)
        return -1;

    if(!(card_info.ocr & (1<<30)))
        start <<= 9; /* not SDHC */

    while (retry_cnt++ < 20)
    {
        cnt = count;
        dst = (unsigned char *)buf;

        ret = 0;
        retry = false;       /* reset retry flag */

        mmu_buff_reset();

        if (cnt == 1)
        {
            /* last block to transfer */
            SD_DATAT = DATA_XFER_START | DATA_XFER_READ |
                       DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
                       DATA_XFER_SINGLE;
        }
        else
        {
            /* more than one block to transfer */
            SD_DATAT = DATA_XFER_START | DATA_XFER_READ |
                       DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
                       DATA_XFER_MULTI;
        }

        /* issue read command to the card */
        if (!send_cmd(SD_READ_MULTIPLE_BLOCK, start, RES_R1, &response))
        {
            ret = -2;
            continue;
        }

        while (cnt > 0)
        {
#ifdef RK27XX_SD_DEBUG
            /* debug stuff */
            TMR1LR = 0xffffffff;
#endif
            /* wait for transfer completion */
            semaphore_wait(&transfer_completion_signal, TIMEOUT_BLOCK);

#ifdef RK27XX_SD_DEBUG
            /* debug stuff */
            sd_debug_time_rd = 0xffffffff - TMR1CVR;
#endif
            if (retry)
            {
                /* data transfer error */
                ret = -3;
                break;
            }

            /* exchange buffers */
            mmu_switch_buff();

            cnt--;

            if (cnt == 0)
            {
                if (!send_cmd(SD_STOP_TRANSMISSION, 0, RES_R1b, &response))
                    ret = -4;

                read_sd_data(&dst);

                break;
            }
            else if (cnt == 1)
            {
                /* last block to transfer */
                SD_DATAT = DATA_XFER_START | DATA_XFER_READ |
                           DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
                           DATA_XFER_SINGLE;

                read_sd_data(&dst);

            }
            else
            {
                /* more than one block to transfer */
                SD_DATAT = DATA_XFER_START | DATA_XFER_READ |
                           DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
                           DATA_XFER_MULTI;

                read_sd_data(&dst);
            }

            last_disk_activity = current_tick;

        } /* while (cnt > 0) */

        /* transfer successfull - leave retry loop */
        if (ret == 0)
            break;

    } /* while (retry_cnt++ < 20) */

    sd_enable(false);
    mutex_unlock(&sd_mtx);

    return ret;
}

/* Not tested */
int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
                     const void* buf)
{
#ifdef HAVE_MULTIDRIVE
    (void) drive;
#endif
#if defined(BOOTLOADER) /* we don't need write support in bootloader */
    (void) start;
    (void) count;
    (void) buf;
    return -1;
#else

#ifdef RK27XX_SD_DEBUG
    /* debug stuff */
    TMR1LR = 0xffffffff;
#endif

    unsigned long response;
    unsigned int retry_cnt = 0;
    int cnt, ret = 0;
    unsigned char *src;
    /* bool card_selected = false; */

    mutex_lock(&sd_mtx);
    sd_enable(true);

    if (count <= 0 || start + count > card_info.numblocks)
        return -1;

    if(!(card_info.ocr & (1<<30)))
        start <<= 9; /* not SDHC */

    while (retry_cnt++ < 20)
    {
        cnt = count;
        src = (unsigned char *)buf;

        ret = 0;
        retry = false;       /* reset retry flag */
        mmu_buff_reset();    /* reset recive buff state */

        write_sd_data(&src); /* put data into transfer buffer */

        if (!send_cmd(SD_WRITE_MULTIPLE_BLOCK, start, RES_R1, &response))
        {
            ret = -3;
            continue;
        }

        while (cnt > 0)
        {
            /* exchange buffers */
            mmu_switch_buff();

            if (cnt == 1)
            {
                /* last block to transfer */
                SD_DATAT = DATA_XFER_START | DATA_XFER_WRITE |
                           DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
                           DATA_XFER_SINGLE;

            }
            else
            {
                /* more than one block to transfer */
                SD_DATAT = DATA_XFER_START | DATA_XFER_WRITE |
                           DATA_BUS_1LINE | DATA_XFER_DMA_DIS |
                           DATA_XFER_MULTI;

                /* put more data */
                write_sd_data(&src);
            }
            /* wait for transfer completion */
            semaphore_wait(&transfer_completion_signal, TIMEOUT_BLOCK);

            if (retry)
            {
                /* data transfer error */
                ret = -3;
                break;
            }

            cnt--;
        } /* while (cnt > 0) */

        if (!send_cmd(SD_STOP_TRANSMISSION, 0, RES_R1b, &response))
            ret = -4;

        if (!sd_wait_card_busy())
            ret = -5;

        /* transfer successfull - leave retry loop */
        if (ret == 0)
            break;
    }

    sd_enable(false);
    mutex_unlock(&sd_mtx);

#ifdef RK27XX_SD_DEBUG
    /* debug stuff */
    sd_debug_time_wr = 0xffffffff - TMR1CVR;
#endif

    return ret;
   
#endif /* defined(BOOTLOADER) */
}

void sd_enable(bool on)
{
    /* enable or disable clock signal for SD module */
    if (on)
    {
        SCU_CLKCFG &= ~CLKCFG_SD;
        led(true);
    }
    else
    {
        SCU_CLKCFG |= CLKCFG_SD;
        led(false);
    }
}

#ifndef BOOTLOADER
long sd_last_disk_activity(void)
{
    return last_disk_activity;
}

tCardInfo *card_get_info_target(int card_no)
{
    (void)card_no;
    return &card_info;
}
#endif /* BOOTLOADER */

#ifdef HAVE_HOTSWAP
/* Not complete and disabled in config */
bool sd_removable(IF_MD_NONVOID(int drive))
{
    (void)drive;
    return true;
}

bool sd_present(IF_MD_NONVOID(int drive))
{
    (void)drive;
    return card_detect_target();
}

static int sd_oneshot_callback(struct timeout *tmo)
{
    (void)tmo;

    /* This is called only if the state was stable for 300ms - check state
     * and post appropriate event. */
    if (card_detect_target())
    {
        queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
    }
    else
        queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);

    return 0;
}

/* interrupt handler for SD detect */

#endif /* HAVE_HOTSWAP */

#ifdef CONFIG_STORAGE_MULTI
int sd_num_drives(int first_drive)
{
    (void)first_drive;

    /* we have only one SD drive */
    return 1;
}
#endif /* CONFIG_STORAGE_MULTI */