1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2009 by Bob Cousins
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
27 #include "gcc_extensions.h"
32 #include "uart-s3c2440.h"
39 #include "dma-target.h"
40 #include "system-target.h"
41 #include "led-mini2440.h"
43 /* The configuration method is not very flexible. */
44 #define CARD_NUM_SLOT 0
50 #define EC_WAIT_STATE_FAILED 3
52 #define EC_FIFO_WR_EMPTY 5
53 #define EC_FIFO_WR_DONE 6
54 #define EC_TRAN_READ_ENTRY 7
55 #define EC_TRAN_READ_EXIT 8
56 #define EC_TRAN_WRITE_ENTRY 9
57 #define EC_TRAN_WRITE_EXIT 10
59 #define EC_WRITE_PROTECT 12
61 #define MIN_YIELD_PERIOD 1000
62 #define UNALIGNED_NUM_SECTORS 10
63 #define MAX_TRANSFER_ERRORS 10
65 /* command flags for send_cmd */
66 #define MCI_NO_FLAGS (0<<0)
67 #define MCI_RESP (1<<0)
68 #define MCI_LONG_RESP (1<<1)
69 #define MCI_ARG (1<<2)
71 #define INITIAL_CLK 400000 /* Initial clock */
72 #define SD_CLK 24000000 /* Clock for SD cards */
73 #define MMC_CLK 15000000 /* Clock for MMC cards */
75 #define SD_ACTIVE_LED LED4
78 #define dbgprintf uart_printf
80 #define dbgprintf(...)
89 /** static, private data **/
91 /* for compatibility */
92 static long last_disk_activity
= -1;
94 static bool initialized
= false;
95 static bool sd_enabled
= false;
96 static long next_yield
= 0;
98 static tCardInfo card_info
[NUM_CARDS
];
100 #ifdef HAVE_MULTIDRIVE
101 static int curr_card
= 0; /* current active card */
103 static struct sd_card_status sd_status
[NUM_CARDS
] =
113 /* Shoot for around 75% usage */
114 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x1c0)/sizeof(long)];
115 static const char sd_thread_name
[] = "sd";
116 static struct mutex sd_mtx SHAREDBSS_ATTR
;
117 static struct event_queue sd_queue
;
118 static struct semaphore transfer_completion_signal
;
119 static volatile unsigned int transfer_error
[NUM_DRIVES
];
120 /* align on cache line size */
121 static unsigned char aligned_buffer
[UNALIGNED_NUM_SECTORS
* SD_BLOCK_SIZE
]
122 __attribute__((aligned(32)));
123 static unsigned char * uncached_buffer
;
125 static inline void mci_delay(void)
129 asm volatile ("nop\n");
132 /* TODO: should be in target include file */
133 /*****************************************************************************
134 Definitions specific to Mini2440
135 *****************************************************************************/
137 #define SD_CD (1<<8) /* Port G */
138 #define SD_WP (1<<8) /* Port H */
140 /*****************************************************************************
141 Functions specific to S3C2440 SoC
142 *****************************************************************************/
145 static unsigned reg_copy
[16], reg_copy2
[16];
146 static void get_regs (unsigned *regs
)
149 volatile unsigned long *sdi_reg
= &SDICON
;
151 for (j
=0; j
< 16;j
++)
153 *regs
++ = *sdi_reg
++;
157 static void dump_regs (unsigned *regs1
, unsigned *regs2
)
160 volatile unsigned long*sdi_reg
= &SDICON
;
163 for (j
=0; j
< 16;j
++)
165 diff
= *regs1
^ *regs2
;
167 dbgprintf ("%8x %8x %8x %8x\n", sdi_reg
, *regs1
, *regs2
, diff
);
175 static void debug_r1(int cmd
)
177 #if defined(SD_DEBUG)
178 dbgprintf("CMD%2.2d:SDICSTA=%04x [%c%c%c%c%c-%c%c%c%c%c%c%c] SDIRSP0=%08x [%d %s] \n",
181 (SDICSTA
& S3C2410_SDICMDSTAT_CRCFAIL
) ? 'C' : ' ',
182 (SDICSTA
& S3C2410_SDICMDSTAT_CMDSENT
) ? 'S' : ' ',
183 (SDICSTA
& S3C2410_SDICMDSTAT_CMDTIMEOUT
) ? 'T' : ' ',
184 (SDICSTA
& S3C2410_SDICMDSTAT_RSPFIN
) ? 'R' : ' ',
185 (SDICSTA
& S3C2410_SDICMDSTAT_XFERING
) ? 'X' : ' ',
187 (SDICSTA
& 0x40) ? 'P' : ' ',
188 (SDICSTA
& 0x20) ? 'A' : ' ',
189 (SDICSTA
& 0x10) ? 'E' : ' ',
190 (SDICSTA
& 0x08) ? 'C' : ' ',
191 (SDICSTA
& 0x04) ? 'I' : ' ',
192 (SDICSTA
& 0x02) ? 'R' : ' ',
193 (SDICSTA
& 0x01) ? 'Z' : ' ',
196 SD_R1_CURRENT_STATE(SDIRSP0
),
197 (SDIRSP0
& SD_R1_READY_FOR_DATA
) ? "RDY " : " "
206 int status
= SDIDSTA
;
207 #ifndef HAVE_MULTIDRIVE
208 const int curr_card
= 0;
211 transfer_error
[curr_card
] = status
213 & ( S3C2410_SDIDSTA_CRCFAIL
| S3C2410_SDIDSTA_RXCRCFAIL
|
214 S3C2410_SDIDSTA_DATATIMEOUT
)
218 SDIDSTA
|= S3C2410_SDIDSTA_CLEAR_BITS
; /* needed to clear int */
220 dbgprintf ("SDI %x\n", transfer_error
[curr_card
]);
222 semaphore_release(&transfer_completion_signal
);
224 /* Ack the interrupt */
230 void dma_callback (void)
232 const int status
= SDIDSTA
;
234 transfer_error
[0] = status
& (S3C2410_SDIDSTA_CRCFAIL
|
235 S3C2410_SDIDSTA_RXCRCFAIL
|
236 S3C2410_SDIDSTA_DATATIMEOUT
);
238 SDIDSTA
|= S3C2410_SDIDSTA_CLEAR_BITS
; /* needed to clear int */
240 dbgprintf ("dma_cb\n");
241 semaphore_release(&transfer_completion_signal
);
245 static void init_sdi_controller(const int card_no
)
249 /*****************************************************************************/
251 /* Specific to Mini2440 */
253 /* Enable pullups on SDCMD and SDDAT pins */
254 S3C2440_GPIO_PULLUP (GPEUP
, 6, GPIO_PULLUP_ENABLE
);
255 S3C2440_GPIO_PULLUP (GPEUP
, 7, GPIO_PULLUP_ENABLE
);
256 S3C2440_GPIO_PULLUP (GPEUP
, 8, GPIO_PULLUP_ENABLE
);
257 S3C2440_GPIO_PULLUP (GPEUP
, 9, GPIO_PULLUP_ENABLE
);
258 S3C2440_GPIO_PULLUP (GPEUP
, 10, GPIO_PULLUP_ENABLE
);
260 /* Enable special function for SDCMD, SDCLK and SDDAT pins */
261 S3C2440_GPIO_CONFIG (GPECON
, 5, GPIO_FUNCTION
);
262 S3C2440_GPIO_CONFIG (GPECON
, 6, GPIO_FUNCTION
);
263 S3C2440_GPIO_CONFIG (GPECON
, 7, GPIO_FUNCTION
);
264 S3C2440_GPIO_CONFIG (GPECON
, 8, GPIO_FUNCTION
);
265 S3C2440_GPIO_CONFIG (GPECON
, 9, GPIO_FUNCTION
);
266 S3C2440_GPIO_CONFIG (GPECON
, 10, GPIO_FUNCTION
);
268 /* Card Detect input */
269 S3C2440_GPIO_CONFIG (GPGCON
, 8, GPIO_INPUT
);
270 /* enable external irq 8-23 on the internal interrupt controller */
272 /* enable GPG8 IRQ on the external interrupt controller */
273 EINTMASK
&= ~(1<<16);
276 /* Write Protect input */
277 S3C2440_GPIO_CONFIG (GPHCON
, 8, GPIO_INPUT
);
278 /*****************************************************************************/
280 #error Unsupported target
282 /*****************************************************************************/
284 /* About 400KHz for initial comms with card */
285 SDIPRE
= PCLK
/ INITIAL_CLK
- 1;
286 /* Byte order=Type A (Little Endian), clock enable */
287 SDICON
= S3C2410_SDICON_CLOCKTYPE
;
288 SDIFSTA
|= S3C2440_SDIFSTA_FIFORESET
;
289 SDIBSIZE
= SD_BLOCK_SIZE
;
290 SDIDTIMER
= 0x7fffff; /* Set timeout count - max value */
292 /* Enable interupt on Data Finish or data transfer error */
293 /* Clear pending source */
298 /* Enable interrupt in controller */
299 bitclr32(&INTMOD
, SDI_MASK
);
300 bitclr32(&INTMSK
, SDI_MASK
);
302 SDIIMSK
|= S3C2410_SDIIMSK_DATAFINISH
303 | S3C2410_SDIIMSK_DATATIMEOUT
304 | S3C2410_SDIIMSK_DATACRC
305 | S3C2410_SDIIMSK_CRCSTATUS
306 | S3C2410_SDIIMSK_FIFOFAIL
311 static bool send_cmd(const int card_no
, const int cmd
, const int arg
,
312 const int flags
, long *response
)
315 unsigned val
, status
;
321 /* A major bodge. For some reason a delay is required here */
323 dbgprintf ("send_cmd: c=%3.3d a=%08x f=%02x \n", cmd
, arg
, flags
);
326 get_regs (reg_copy2
);
327 dump_regs (reg_copy
, reg_copy2
);
331 while (SDICSTA
& S3C2410_SDICMDSTAT_XFERING
)
334 /* set up new command */
341 val
= cmd
| S3C2410_SDICMDCON_CMDSTART
| S3C2410_SDICMDCON_SENDERHOST
;
344 val
|= S3C2410_SDICMDCON_WAITRSP
;
345 if(flags
& MCI_LONG_RESP
)
346 val
|= S3C2410_SDICMDCON_LONGRSP
;
349 /* Clear command/data status flags */
350 SDICSTA
|= 0x0f << 9;
351 SDIDSTA
|= S3C2410_SDIDSTA_CLEAR_BITS
;
353 /* Initiate the command */
356 if (flags
& MCI_RESP
)
358 /* wait for response or timeout */
362 } while ( (status
& (S3C2410_SDICMDSTAT_RSPFIN
|
363 S3C2410_SDICMDSTAT_CMDTIMEOUT
) ) == 0);
365 if (status
& S3C2410_SDICMDSTAT_CMDTIMEOUT
)
367 else if (status
& (S3C2410_SDICMDSTAT_RSPFIN
))
370 if(flags
& MCI_LONG_RESP
)
372 /* store the response in reverse word order */
373 response
[0] = SDIRSP3
;
374 response
[1] = SDIRSP2
;
375 response
[2] = SDIRSP1
;
376 response
[3] = SDIRSP0
;
379 response
[0] = SDIRSP0
;
387 /* wait for command completion or timeout */
391 } while ( (status
& (S3C2410_SDICMDSTAT_CMDSENT
|
392 S3C2410_SDICMDSTAT_CMDTIMEOUT
)) == 0);
394 if (status
& S3C2410_SDICMDSTAT_CMDTIMEOUT
)
400 /* Clear Command status flags */
401 SDICSTA
|= 0x0f << 9;
408 static int sd_init_card(const int card_no
)
410 unsigned long temp_reg
[4];
411 unsigned long response
;
416 if(!send_cmd(card_no
, SD_GO_IDLE_STATE
, 0, MCI_NO_FLAGS
, NULL
))
422 if(send_cmd(card_no
, SD_SEND_IF_COND
, 0x1AA, MCI_RESP
|MCI_ARG
, &response
))
423 if((response
& 0xFFF) == 0x1AA)
426 /* timeout for initialization is 1sec, from SD Specification 2.00 */
427 init_timeout
= current_tick
+ HZ
;
431 if(current_tick
> init_timeout
)
435 if( !send_cmd(card_no
, SD_APP_CMD
, 0, MCI_RESP
|MCI_ARG
, &response
) ||
436 !(response
& (1<<5)) )
442 if(!send_cmd(card_no
, SD_APP_OP_COND
, (sdhc
? 0x40FF8000 : (1<<23)),
443 MCI_RESP
|MCI_ARG
, &card_info
[card_no
].ocr
))
448 } while(!(card_info
[card_no
].ocr
& (1<<31)));
451 if(!send_cmd(card_no
, SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
|MCI_ARG
,
456 card_info
[card_no
].cid
[3-i
] = temp_reg
[i
];
459 if(!send_cmd(card_no
, SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
|MCI_ARG
,
460 &card_info
[card_no
].rca
))
464 if(!send_cmd(card_no
, SD_SEND_CSD
, card_info
[card_no
].rca
,
465 MCI_RESP
|MCI_LONG_RESP
|MCI_ARG
, temp_reg
))
469 card_info
[card_no
].csd
[3-i
] = temp_reg
[i
];
471 sd_parse_csd(&card_info
[card_no
]);
473 if(!send_cmd(card_no
, SD_SELECT_CARD
, card_info
[card_no
].rca
, MCI_ARG
, NULL
))
476 if(!send_cmd(card_no
, SD_APP_CMD
, card_info
[card_no
].rca
, MCI_ARG
, NULL
))
479 if(!send_cmd(card_no
, SD_SET_BUS_WIDTH
, card_info
[card_no
].rca
| 2, MCI_ARG
, NULL
))
482 if(!send_cmd(card_no
, SD_SET_BLOCKLEN
, card_info
[card_no
].blocksize
, MCI_ARG
,
486 card_info
[card_no
].initialized
= 1;
488 /* full speed for controller clock */
489 SDIPRE
= PCLK
/ SD_CLK
- 1;
495 /*****************************************************************************
497 *****************************************************************************/
499 static inline bool card_detect_target(void)
501 /* TODO - use interrupt on change? */
503 return (GPGDAT
& SD_CD
) == 0;
505 #error Unsupported target
510 /*****************************************************************************/
513 static int sd1_oneshot_callback(struct timeout
*tmo
)
517 /* This is called only if the state was stable for 300ms - check state
518 * and post appropriate event. */
519 if (card_detect_target())
521 queue_broadcast(SYS_HOTSWAP_INSERTED
, 0);
524 queue_broadcast(SYS_HOTSWAP_EXTRACTED
, 0);
530 static struct timeout sd1_oneshot
;
531 EINTPEND
= (1<<16); /* ack irq on external, then internal irq controller */
534 /* add task to inform the system about the SD insertion
535 * sanity check if it's still inserted after 300ms */
536 timeout_register(&sd1_oneshot
, sd1_oneshot_callback
, (3*HZ
/10), 0);
539 bool sd_removable(IF_MD_NONVOID(int card_no
))
541 #ifndef HAVE_MULTIDRIVE
542 const int card_no
= 0;
544 dbgprintf ("sd_remov (hs) [%d] %d\n", card_no
, card_no
== CARD_NUM_SLOT
);
545 return (card_no
== CARD_NUM_SLOT
);
548 bool sd_present(IF_MD_NONVOID(int card_no
))
550 #ifdef HAVE_MULTIDRIVE
553 dbgprintf ("sd_pres (hs) [%d] %d\n", card_no
, card_detect_target());
554 return card_detect_target();
557 /*****************************************************************************/
560 bool sd_removable(IF_MD_NONVOID(int card_no
))
562 #ifndef HAVE_MULTIDRIVE
563 const int card_no
= 0;
568 dbgprintf ("sd_remov");
572 #endif /* HAVE_HOTSWAP */
573 /*****************************************************************************/
575 static void sd_thread(void) NORETURN_ATTR
;
576 static void sd_thread(void)
578 struct queue_event ev
;
583 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
587 case SYS_HOTSWAP_INSERTED
:
588 case SYS_HOTSWAP_EXTRACTED
:
591 fat_lock(); /* lock-out FAT activity first -
592 prevent deadlocking via disk_mount that
593 would cause a reverse-order attempt with
595 mutex_lock(&sd_mtx
); /* lock-out card activity - direct calls
596 into driver that bypass the fat cache */
598 /* We now have exclusive control of fat cache and ata */
600 disk_unmount(0); /* release "by force", ensure file
601 descriptors aren't leaked and any busy
602 ones are invalid if mounting */
604 /* Force card init for new card, re-init for re-inserted one or
605 * clear if the last attempt to init failed with an error. */
606 card_info
[0].initialized
= 0;
608 if (ev
.id
== SYS_HOTSWAP_INSERTED
)
610 /* FIXME: once sd_enabled is implement properly,
611 * reinitializing the controllers might be needed */
613 if (success
< 0) /* initialisation failed */
614 panicf("SD init failed : %d", success
);
615 success
= disk_mount(0); /* 0 if fail */
618 /* notify the system about the changed filesystems
621 queue_broadcast(SYS_FS_CHANGED
, 0);
623 /* Access is now safe */
624 mutex_unlock(&sd_mtx
);
634 static int sd_wait_for_state(const int card_no
, unsigned int state
)
636 unsigned long response
= 0;
637 unsigned int timeout
= HZ
; /* ticks */
638 long t
= current_tick
;
644 if(!send_cmd(card_no
, SD_SEND_STATUS
, card_info
[card_no
].rca
,
645 MCI_RESP
|MCI_ARG
, &response
))
648 if( (SD_R1_CURRENT_STATE(response
) == state
) )
651 if(TIME_AFTER(current_tick
, t
+ timeout
))
654 if (TIME_AFTER((tick
= current_tick
), next_yield
))
657 timeout
+= current_tick
- tick
;
658 next_yield
= tick
+ MIN_YIELD_PERIOD
;
663 static int sd_transfer_sectors(int card_no
, unsigned long start
,
664 int count
, void* buf
, const bool write
)
668 struct dma_request request
;
672 set_leds(SD_ACTIVE_LED
);
674 #ifdef HAVE_MULTIDRIVE
677 if (card_info
[card_no
].initialized
<= 0)
679 ret
= sd_init_card(card_no
);
680 if (!(card_info
[card_no
].initialized
))
681 goto sd_transfer_error
;
684 last_disk_activity
= current_tick
;
686 ret
= sd_wait_for_state(card_no
, SD_TRAN
);
690 goto sd_transfer_error
;
697 /* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH
698 * register, so we have to transfer maximum 127 sectors at a time. */
699 unsigned int transfer
= (count
>= 128) ? 127 : count
; /* sectors */
702 write
? SD_WRITE_MULTIPLE_BLOCK
: SD_READ_MULTIPLE_BLOCK
;
703 unsigned long start_addr
= start
;
705 dma_buf
= aligned_buffer
;
706 if(transfer
> UNALIGNED_NUM_SECTORS
)
707 transfer
= UNALIGNED_NUM_SECTORS
;
709 memcpy(uncached_buffer
, buf
, transfer
* SD_BLOCK_SIZE
);
711 /* Set start_addr to the correct unit (blocks or bytes) */
712 if(!(card_info
[card_no
].ocr
& SD_OCR_CARD_CAPACITY_STATUS
))/* not SDHC */
713 start_addr
*= SD_BLOCK_SIZE
;
716 SDIFSTA
= SDIFSTA
| S3C2440_SDIFSTA_FIFORESET
;
717 SDIDCON
= S3C2440_SDIDCON_DS_WORD
|
718 S3C2410_SDIDCON_BLOCKMODE
| S3C2410_SDIDCON_WIDEBUS
|
719 S3C2410_SDIDCON_DMAEN
|
720 S3C2440_SDIDCON_DATSTART
|
723 SDIDCON
|= S3C2410_SDIDCON_TXAFTERRESP
| S3C2410_SDIDCON_XFER_TXSTART
;
725 SDIDCON
|= S3C2410_SDIDCON_RXAFTERCMD
| S3C2410_SDIDCON_XFER_RXSTART
;
727 SDIDSTA
|= S3C2410_SDIDSTA_CLEAR_BITS
; /* needed to clear int */
731 /* Initiate read/write command */
732 if(!send_cmd(card_no
, cmd
, start_addr
, MCI_ARG
| MCI_RESP
, NULL
))
735 goto sd_transfer_error
;
740 request
.source_addr
= dma_buf
;
741 request
.source_control
= DISRCC_LOC_AHB
| DISRCC_INC_AUTO
;
742 request
.dest_addr
= &SDIDAT_LLE
;
743 request
.dest_control
= DISRCC_LOC_APB
| DISRCC_INC_FIXED
;
744 request
.count
= transfer
* SD_BLOCK_SIZE
/ sizeof(long);
745 request
.source_map
= DMA_SRC_MAP_SDI
;
746 request
.control
= DCON_DMD_HS
| DCON_SYNC_APB
|
748 DCON_NO_RELOAD
| DCON_DSZ_WORD
;
749 request
.callback
= NULL
;
751 dma_enable_channel(0, &request
);
755 request
.source_addr
= &SDIDAT_LLE
;
756 request
.source_control
= DISRCC_LOC_APB
| DISRCC_INC_FIXED
;
757 request
.dest_addr
= dma_buf
;
758 request
.dest_control
= DISRCC_LOC_AHB
| DISRCC_INC_AUTO
;
759 request
.count
= transfer
* SD_BLOCK_SIZE
/ sizeof(long);
760 request
.source_map
= DMA_SRC_MAP_SDI
;
761 request
.control
= DCON_DMD_HS
| DCON_SYNC_APB
|
763 DCON_NO_RELOAD
| DCON_DSZ_WORD
;
764 request
.callback
= NULL
;
766 dma_enable_channel(0, &request
);
770 /* FIXME : we should check if the timeouts calculated from the card's
771 * CSD are lower, and use them if it is the case
772 * Note : the OF doesn't seem to use them anyway */
773 MCI_DATA_TIMER(drive
) = write
?
774 SD_MAX_WRITE_TIMEOUT
: SD_MAX_READ_TIMEOUT
;
775 MCI_DATA_LENGTH(drive
) = transfer
* card_info
[drive
].blocksize
;
776 MCI_DATA_CTRL(drive
) = (1<<0) /* enable */ |
777 (!write
<<1) /* transfer direction */ |
779 (9<<4) /* 2^9 = 512 */ ;
782 semaphore_wait(&transfer_completion_signal
, 100 /*TIMEOUT_BLOCK*/);
784 /* wait for DMA to finish */
785 while (DSTAT0
& DSTAT_STAT_BUSY
)
790 while ((status
& (S3C2410_SDIDSTA_DATATIMEOUT
|S3C2410_SDIDSTA_XFERFINISH
)) == 0)
794 dbgprintf("%x \n", status
);
796 if( transfer_error
[card_no
] & S3C2410_SDIDSTA_XFERFINISH
)
799 memcpy(buf
, uncached_buffer
, transfer
* SD_BLOCK_SIZE
);
800 buf
+= transfer
* SD_BLOCK_SIZE
;
803 loops
= 0; /* reset errors counter */
807 dbgprintf ("SD transfer error : 0x%x\n", transfer_error
[card_no
]);
809 if(loops
++ > MAX_TRANSFER_ERRORS
)
811 led_flash(LED1
|LED2
, LED3
|LED4
);
812 /* panicf("SD transfer error : 0x%x", transfer_error[card_no]); */
816 last_disk_activity
= current_tick
;
818 if(!send_cmd(card_no
, SD_STOP_TRANSMISSION
, 0, MCI_RESP
, NULL
))
821 goto sd_transfer_error
;
825 ret
= sd_wait_for_state(card_no
, SD_TRAN
);
829 goto sd_transfer_error
;
840 clear_leds(SD_ACTIVE_LED
);
844 card_info
[card_no
].initialized
= 0;
846 mutex_unlock(&sd_mtx
);
850 int sd_read_sectors(IF_MD2(int card_no
,) unsigned long start
, int incount
,
855 #ifdef HAVE_MULTIDRIVE
856 dbgprintf ("sd_read %d %x %d\n", card_no
, start
, incount
);
858 dbgprintf ("sd_read %x %d\n", start
, incount
);
860 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
861 if (!card_detect_target())
862 ret
= 0; /* assume success */
865 ret
= sd_transfer_sectors(card_no
, start
, incount
, inbuf
, false);
866 dbgprintf ("sd_read, ret=%d\n", ret
);
870 /*****************************************************************************/
871 int sd_write_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
874 #ifdef BOOTLOADER /* we don't need write support in bootloader */
875 #ifdef HAVE_MULTIDRIVE
883 #ifdef HAVE_MULTIDRIVE
884 dbgprintf ("sd_write %d %x %d\n", drive
, start
, count
);
886 dbgprintf ("sd_write %x %d\n", start
, count
);
888 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
889 if (!card_detect_target())
890 return 0; /* assume success */
893 return sd_transfer_sectors(drive
, start
, count
, (void*)outbuf
, true);
896 /*****************************************************************************/
898 void sd_enable(bool on
)
900 dbgprintf ("sd_enable %d\n", on
);
901 /* TODO: enable/disable SDI clock */
903 if (sd_enabled
== on
)
904 return; /* nothing to do */
918 dbgprintf ("\n==============================\n");
919 dbgprintf (" sd_init\n");
920 dbgprintf ("==============================\n");
922 init_sdi_controller (0);
927 semaphore_init(&transfer_completion_signal
, 1, 0);
930 queue_init(&sd_queue
, true);
931 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
932 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
) IF_COP(, CPU
));
934 uncached_buffer
= UNCACHED_ADDR(&aligned_buffer
[0]);
938 * prepare detecting of SD insertion (not extraction) */
939 unsigned long for_extint
= EXTINT2
;
940 unsigned long for_gpgcon
= GPGCON
;
942 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
943 for_extint
|= 0x2; /* detect falling edge only (0 means SD inserted) */
945 for_extint
|= 0x3; /* detect both, raising and falling, edges */
947 for_gpgcon
&= ~(0x3<<16);
948 for_gpgcon
|= (0x2<<16); /* enable interrupt on pin 8 */
949 EXTINT2
= for_extint
;
957 long sd_last_disk_activity(void)
959 return last_disk_activity
;
962 tCardInfo
*card_get_info_target(int card_no
)
964 return &card_info
[card_no
];
967 /*****************************************************************************/
968 #ifdef CONFIG_STORAGE_MULTI
970 int sd_num_drives(int first_drive
)
972 dbgprintf ("sd_num_drv");
974 /* Store which logical drive number(s) we have been assigned */
975 sd_first_drive
= first_drive
;
981 void sd_sleepnow(void)
985 bool sd_disk_is_active(void)
990 int sd_soft_reset(void)
995 int sd_spinup_time(void)
1000 #endif /* CONFIG_STORAGE_MULTI */
1001 /*****************************************************************************/