1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Daniel Ankers
11 * Copyright © 2008-2009 Rafaël Carré
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 /* Driver for the ARM PL180 SD/MMC controller inside AS3525 SoC */
25 /* TODO: Find the real capacity of >2GB models (will be useful for USB) */
27 #include "config.h" /* for HAVE_MULTIDRIVE & AMS_OF_SIZE */
38 #include "pl180.h" /* SD controller */
39 #include "pl081.h" /* DMA controller */
40 #include "dma-target.h" /* DMA request lines */
41 #include "clock-target.h"
43 #ifdef HAVE_BUTTON_LIGHT
44 #include "backlight-target.h"
47 #include "ata_idle_notify.h"
56 #define MCI_NO_FLAGS (0<<0)
57 #define MCI_RESP (1<<0)
58 #define MCI_LONG_RESP (1<<1)
59 #define MCI_ARG (1<<2)
61 /* ARM PL180 registers */
62 #define MCI_POWER(i) (*(volatile unsigned char *) (pl180_base[i]+0x00))
63 #define MCI_CLOCK(i) (*(volatile unsigned long *) (pl180_base[i]+0x04))
64 #define MCI_ARGUMENT(i) (*(volatile unsigned long *) (pl180_base[i]+0x08))
65 #define MCI_COMMAND(i) (*(volatile unsigned long *) (pl180_base[i]+0x0C))
66 #define MCI_RESPCMD(i) (*(volatile unsigned long *) (pl180_base[i]+0x10))
67 #define MCI_RESP0(i) (*(volatile unsigned long *) (pl180_base[i]+0x14))
68 #define MCI_RESP1(i) (*(volatile unsigned long *) (pl180_base[i]+0x18))
69 #define MCI_RESP2(i) (*(volatile unsigned long *) (pl180_base[i]+0x1C))
70 #define MCI_RESP3(i) (*(volatile unsigned long *) (pl180_base[i]+0x20))
71 #define MCI_DATA_TIMER(i) (*(volatile unsigned long *) (pl180_base[i]+0x24))
72 #define MCI_DATA_LENGTH(i) (*(volatile unsigned short*) (pl180_base[i]+0x28))
73 #define MCI_DATA_CTRL(i) (*(volatile unsigned char *) (pl180_base[i]+0x2C))
74 #define MCI_DATA_CNT(i) (*(volatile unsigned short*) (pl180_base[i]+0x30))
75 #define MCI_STATUS(i) (*(volatile unsigned long *) (pl180_base[i]+0x34))
76 #define MCI_CLEAR(i) (*(volatile unsigned long *) (pl180_base[i]+0x38))
77 #define MCI_MASK0(i) (*(volatile unsigned long *) (pl180_base[i]+0x3C))
78 #define MCI_MASK1(i) (*(volatile unsigned long *) (pl180_base[i]+0x40))
79 #define MCI_SELECT(i) (*(volatile unsigned long *) (pl180_base[i]+0x44))
80 #define MCI_FIFO_CNT(i) (*(volatile unsigned long *) (pl180_base[i]+0x48))
83 (MCI_DATA_CRC_FAIL | MCI_DATA_TIMEOUT | MCI_RX_OVERRUN | MCI_TX_UNDERRUN)
85 #define MCI_FIFO(i) ((unsigned long *) (pl180_base[i]+0x80))
87 #define INTERNAL_AS3525 0 /* embedded SD card */
88 #define SD_SLOT_AS3525 1 /* SD slot if present */
90 static const int pl180_base
[NUM_DRIVES
] = {
92 #ifdef HAVE_MULTIDRIVE
97 static int sd_wait_for_state(const int drive
, unsigned int state
);
98 static int sd_select_bank(signed char bank
);
99 static int sd_init_card(const int drive
);
100 static void init_pl180_controller(const int drive
);
102 #define BLOCKS_PER_BANK 0x7a7800
104 static tCardInfo card_info
[NUM_DRIVES
];
106 /* maximum timeouts recommanded in the SD Specification v2.00 */
107 #define SD_MAX_READ_TIMEOUT ((AS3525_PCLK_FREQ) / 1000 * 100) /* 100 ms */
108 #define SD_MAX_WRITE_TIMEOUT ((AS3525_PCLK_FREQ) / 1000 * 250) /* 250 ms */
110 /* for compatibility */
111 static long last_disk_activity
= -1;
113 #define MIN_YIELD_PERIOD 5 /* ticks */
114 static long next_yield
= 0;
116 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x200)/sizeof(long)];
117 static const char sd_thread_name
[] = "ata/sd";
118 static struct mutex sd_mtx
;
119 static struct event_queue sd_queue
;
121 bool sd_enabled
= false;
124 static struct wakeup transfer_completion_signal
;
125 static volatile unsigned int transfer_error
[NUM_VOLUMES
];
126 #define PL180_MAX_TRANSFER_ERRORS 10
128 #define UNALIGNED_NUM_SECTORS 10
129 static unsigned char aligned_buffer
[UNALIGNED_NUM_SECTORS
* SD_BLOCK_SIZE
] __attribute__((aligned(32))); /* align on cache line size */
130 static unsigned char *uncached_buffer
= UNCACHED_ADDR(&aligned_buffer
[0]);
132 static inline void mci_delay(void)
136 asm volatile("nop\n");
141 static inline bool card_detect_target(void)
143 #if defined(HAVE_MULTIDRIVE)
144 return !(GPIOA_PIN(2));
151 static int sd1_oneshot_callback(struct timeout
*tmo
)
155 /* This is called only if the state was stable for 300ms - check state
156 * and post appropriate event. */
157 if (card_detect_target())
159 queue_broadcast(SYS_HOTSWAP_INSERTED
, 0);
162 queue_broadcast(SYS_HOTSWAP_EXTRACTED
, 0);
169 static struct timeout sd1_oneshot
;
170 /* acknowledge interrupt */
172 timeout_register(&sd1_oneshot
, sd1_oneshot_callback
, (3*HZ
/10), 0);
174 #endif /* HAVE_HOTSWAP */
178 const int status
= MCI_STATUS(INTERNAL_AS3525
);
180 transfer_error
[INTERNAL_AS3525
] = status
& MCI_ERROR
;
182 wakeup_signal(&transfer_completion_signal
);
183 MCI_CLEAR(INTERNAL_AS3525
) = status
;
186 #ifdef HAVE_MULTIDRIVE
189 const int status
= MCI_STATUS(SD_SLOT_AS3525
);
191 transfer_error
[SD_SLOT_AS3525
] = status
& MCI_ERROR
;
193 wakeup_signal(&transfer_completion_signal
);
194 MCI_CLEAR(SD_SLOT_AS3525
) = status
;
198 static bool send_cmd(const int drive
, const int cmd
, const int arg
,
199 const int flags
, long *response
)
203 /* Clear old status flags */
204 MCI_CLEAR(drive
) = 0x7ff;
206 /* Load command argument or clear if none */
207 MCI_ARGUMENT(drive
) = (flags
& MCI_ARG
) ? arg
: 0;
209 /* Construct MCI_COMMAND & enable CPSM */
212 /* b6 */| ((flags
& (MCI_RESP
|MCI_LONG_RESP
)) ? MCI_COMMAND_RESPONSE
: 0)
213 /* b7 */| ((flags
& MCI_LONG_RESP
) ? MCI_COMMAND_LONG_RESPONSE
: 0)
214 /* b8 | MCI_COMMAND_INTERRUPT */
215 /* b9 | MCI_COMMAND_PENDING */ /*Only used with stream data transfer*/
216 /* b10*/| MCI_COMMAND_ENABLE
; /* Enables CPSM */
218 /* Wait while cmd completes then disable CPSM */
219 while(MCI_STATUS(drive
) & MCI_CMD_ACTIVE
);
220 MCI_COMMAND(drive
) = 0;
222 status
= MCI_STATUS(drive
);
224 /* Handle command responses */
225 if(flags
& MCI_RESP
) /* CMD expects response */
227 response
[0] = MCI_RESP0(drive
); /* Always prepare short response */
229 if(status
& (MCI_CMD_TIMEOUT
| MCI_CMD_CRC_FAIL
)) /* failed response */
232 if(status
& MCI_CMD_RESP_END
) /*Response passed CRC check */
234 if(flags
& MCI_LONG_RESP
)
235 { /* replace short response with long response */
236 /* store the response in reverse words order */
237 response
[0] = MCI_RESP3(drive
);
238 response
[1] = MCI_RESP2(drive
);
239 response
[2] = MCI_RESP1(drive
);
240 response
[3] = MCI_RESP0(drive
);
245 else if(status
& MCI_CMD_SENT
) /* CMD sent, no response required */
251 #define MCI_FULLSPEED (MCI_CLOCK_ENABLE | MCI_CLOCK_BYPASS) /* MCLK */
252 #define MCI_HALFSPEED (MCI_CLOCK_ENABLE) /* MCLK/2 */
253 #define MCI_QUARTERSPEED (MCI_CLOCK_ENABLE | 1) /* MCLK/4 */
254 #define MCI_IDENTSPEED (MCI_CLOCK_ENABLE | AS3525_SD_IDENT_DIV) /* IDENT */
256 static int sd_init_card(const int drive
)
258 unsigned long response
;
261 unsigned long temp_reg
[4];
265 /* MCLCK on and set to 400kHz ident frequency */
266 MCI_CLOCK(drive
) = MCI_IDENTSPEED
;
268 /* 100 - 400kHz clock required for Identification Mode */
269 /* Start of Card Identification Mode ************************************/
272 if(!send_cmd(drive
, SD_GO_IDLE_STATE
, 0, MCI_NO_FLAGS
, NULL
))
277 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
278 Non v2 cards will not respond to this command*/
279 if(send_cmd(drive
, SD_SEND_IF_COND
, 0x1AA, MCI_RESP
|MCI_ARG
, &response
))
280 if((response
& 0xFFF) == 0x1AA)
283 /* timeout for initialization is 1sec, from SD Specification 2.00 */
284 init_timeout
= current_tick
+ HZ
;
287 /* this timeout is the only valid error for this loop*/
288 if(TIME_AFTER(current_tick
, init_timeout
))
292 send_cmd(drive
, SD_APP_CMD
, 0, MCI_RESP
|MCI_ARG
, &response
);
294 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
295 send_cmd(drive
, SD_APP_OP_COND
, (0x00FF8000 | (sd_v2
? 1<<30 : 0)),
296 MCI_RESP
|MCI_ARG
, &card_info
[drive
].ocr
);
298 } while(!(card_info
[drive
].ocr
& (1<<31)));
301 if(!send_cmd(drive
, SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
|MCI_ARG
,
306 card_info
[drive
].cid
[3-i
] = temp_reg
[i
];
309 if(!send_cmd(drive
, SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
|MCI_ARG
,
310 &card_info
[drive
].rca
))
313 /* End of Card Identification Mode ************************************/
315 /* Boost MCICLK to operating speed */ /*FIXME: v1 at 31 MHz still too high*/
316 MCI_CLOCK(drive
) = (sd_v2
? MCI_HALFSPEED
: MCI_HALFSPEED
);
318 #ifdef HAVE_MULTIDRIVE /* The internal SDs are v1 */
320 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
323 /* CMD7 w/rca: Select card to put it in TRAN state */
324 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_ARG
, NULL
))
327 if(sd_wait_for_state(drive
, SD_TRAN
))
330 if(!send_cmd(drive
, SD_SWITCH_FUNC
, 0x80fffff1, MCI_ARG
, NULL
))
334 /* go back to STBY state so we can read csd */
335 /* CMD7 w/rca=0: Deselect card to put it in STBY state */
336 if(!send_cmd(drive
, SD_DESELECT_CARD
, 0, MCI_ARG
, NULL
))
339 #endif /* HAVE_MULTIDRIVE */
342 if(!send_cmd(drive
, SD_SEND_CSD
, card_info
[drive
].rca
,
343 MCI_RESP
|MCI_LONG_RESP
|MCI_ARG
, temp_reg
))
347 card_info
[drive
].csd
[3-i
] = temp_reg
[i
];
349 sd_parse_csd(&card_info
[drive
]);
351 /* CMD7 w/rca: Select card to put it in TRAN state */
352 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_ARG
, NULL
))
356 * enable bank switching
357 * without issuing this command, we only have access to 1/4 of the blocks
358 * of the first bank (0x1E9E00 blocks, which is the size reported in the
361 if(drive
== INTERNAL_AS3525
)
363 const int ret
= sd_select_bank(-1);
368 card_info
[drive
].initialized
= 1;
373 static void sd_thread(void) __attribute__((noreturn
));
374 static void sd_thread(void)
376 struct queue_event ev
;
377 bool idle_notified
= false;
381 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
386 case SYS_HOTSWAP_INSERTED
:
387 case SYS_HOTSWAP_EXTRACTED
:
389 int microsd_init
= 1;
390 fat_lock(); /* lock-out FAT activity first -
391 prevent deadlocking via disk_mount that
392 would cause a reverse-order attempt with
394 mutex_lock(&sd_mtx
); /* lock-out card activity - direct calls
395 into driver that bypass the fat cache */
397 /* We now have exclusive control of fat cache and ata */
399 disk_unmount(SD_SLOT_AS3525
); /* release "by force", ensure file
400 descriptors aren't leaked and any busy
401 ones are invalid if mounting */
403 /* Force card init for new card, re-init for re-inserted one or
404 * clear if the last attempt to init failed with an error. */
405 card_info
[SD_SLOT_AS3525
].initialized
= 0;
407 if (ev
.id
== SYS_HOTSWAP_INSERTED
)
410 init_pl180_controller(SD_SLOT_AS3525
);
411 microsd_init
= sd_init_card(SD_SLOT_AS3525
);
412 if (microsd_init
< 0) /* initialisation failed */
413 panicf("microSD init failed : %d", microsd_init
);
415 microsd_init
= disk_mount(SD_SLOT_AS3525
); /* 0 if fail */
419 * Mount succeeded, or this was an EXTRACTED event,
420 * in both cases notify the system about the changed filesystems
423 queue_broadcast(SYS_FS_CHANGED
, 0);
425 /* Access is now safe */
426 mutex_unlock(&sd_mtx
);
433 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
435 idle_notified
= false;
439 /* never let a timer wrap confuse us */
440 next_yield
= current_tick
;
444 call_storage_idle_notifys(false);
445 idle_notified
= true;
450 case SYS_USB_CONNECTED
:
451 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
452 /* Wait until the USB cable is extracted again */
453 usb_wait_for_disconnect(&sd_queue
);
456 case SYS_USB_DISCONNECTED
:
457 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
463 static void init_pl180_controller(const int drive
)
465 MCI_COMMAND(drive
) = MCI_DATA_CTRL(drive
) = 0;
466 MCI_CLEAR(drive
) = 0x7ff;
468 MCI_MASK0(drive
) = MCI_ERROR
| MCI_DATA_END
;
469 MCI_MASK1(drive
) = 0;
470 #ifdef HAVE_MULTIDRIVE
472 (drive
== INTERNAL_AS3525
) ? INTERRUPT_NAND
: INTERRUPT_MCI0
;
474 /* setup isr for microsd monitoring */
475 VIC_INT_ENABLE
= (INTERRUPT_GPIOA
);
476 /* clear previous irq */
478 /* enable edge detecting */
480 /* detect both raising and falling edges */
484 VIC_INT_ENABLE
= INTERRUPT_NAND
;
487 MCI_POWER(drive
) = MCI_POWER_UP
| (MCI_VDD_3_0
); /* OF Setting */
490 MCI_POWER(drive
) |= MCI_POWER_ON
;
493 MCI_SELECT(drive
) = 0;
495 /* Pl180 clocks get turned on at start of card init */
501 CGU_IDE
= (1<<6) /* enable non AHB interface*/
502 | (AS3525_IDE_DIV
<< 2)
503 | AS3525_CLK_PLLA
; /* clock source = PLLA */
505 CGU_PERI
|= CGU_NAF_CLOCK_ENABLE
;
506 #ifdef HAVE_MULTIDRIVE
507 CGU_PERI
|= CGU_MCI_CLOCK_ENABLE
;
508 CCU_IO
&= ~(1<<3); /* bits 3:2 = 01, xpd is SD interface */
512 wakeup_init(&transfer_completion_signal
);
514 init_pl180_controller(INTERNAL_AS3525
);
515 ret
= sd_init_card(INTERNAL_AS3525
);
518 #ifdef HAVE_MULTIDRIVE
519 init_pl180_controller(SD_SLOT_AS3525
);
525 queue_init(&sd_queue
, true);
526 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
527 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
) IF_COP(, CPU
));
537 bool sd_removable(IF_MD_NONVOID(int drive
))
542 bool sd_present(IF_MD_NONVOID(int drive
))
544 return (drive
== 0) ? true : card_detect_target();
546 #endif /* HAVE_HOTSWAP */
548 static int sd_wait_for_state(const int drive
, unsigned int state
)
550 unsigned long response
= 0;
551 unsigned int timeout
= current_tick
+ 100; /* 100 ticks timeout */
555 if(!send_cmd(drive
, SD_SEND_STATUS
, card_info
[drive
].rca
,
556 MCI_RESP
|MCI_ARG
, &response
))
559 if (((response
>> 9) & 0xf) == state
)
562 if(TIME_AFTER(current_tick
, timeout
))
565 if (TIME_AFTER(current_tick
, next_yield
))
568 next_yield
= current_tick
+ MIN_YIELD_PERIOD
;
573 static int sd_select_bank(signed char bank
)
579 if(loops
++ > PL180_MAX_TRANSFER_ERRORS
)
580 panicf("SD bank %d error : 0x%x", bank
,
581 transfer_error
[INTERNAL_AS3525
]);
583 ret
= sd_wait_for_state(INTERNAL_AS3525
, SD_TRAN
);
587 if(!send_cmd(INTERNAL_AS3525
, SD_SWITCH_FUNC
, 0x80ffffef, MCI_ARG
, NULL
))
592 if(!send_cmd(INTERNAL_AS3525
, 35, 0, MCI_NO_FLAGS
, NULL
))
597 memset(uncached_buffer
, 0, 512);
599 { /* enable bank switching */
600 uncached_buffer
[0] = 16;
601 uncached_buffer
[1] = 1;
602 uncached_buffer
[2] = 10;
605 uncached_buffer
[0] = bank
;
608 /* we don't use the uncached buffer here, because we need the
609 * physical memory address for DMA transfers */
610 dma_enable_channel(0, aligned_buffer
, MCI_FIFO(INTERNAL_AS3525
),
611 DMA_PERI_SD
, DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
,
614 MCI_DATA_TIMER(INTERNAL_AS3525
) = SD_MAX_WRITE_TIMEOUT
;
615 MCI_DATA_LENGTH(INTERNAL_AS3525
) = 512;
616 MCI_DATA_CTRL(INTERNAL_AS3525
) = (1<<0) /* enable */ |
617 (0<<1) /* transfer direction */ |
619 (9<<4) /* 2^9 = 512 */ ;
621 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
623 /* Wait for FIFO to empty, card may still be in PRG state */
624 while(MCI_STATUS(INTERNAL_AS3525
) & MCI_TX_ACTIVE
);
628 } while(transfer_error
[INTERNAL_AS3525
]);
630 card_info
[INTERNAL_AS3525
].current_bank
= (bank
== -1) ? 0 : bank
;
635 static int sd_transfer_sectors(IF_MD2(int drive
,) unsigned long start
,
636 int count
, void* buf
, const bool write
)
638 #ifndef HAVE_MULTIDRIVE
644 /* skip SanDisk OF */
645 if (drive
== INTERNAL_AS3525
)
646 start
+= AMS_OF_SIZE
;
654 if (card_info
[drive
].initialized
<= 0)
656 ret
= sd_init_card(drive
);
657 if (!(card_info
[drive
].initialized
))
658 goto sd_transfer_error
;
661 last_disk_activity
= current_tick
;
667 /* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH
668 * register, so we have to transfer maximum 127 sectors at a time. */
669 unsigned int transfer
= (count
>= 128) ? 127 : count
; /* sectors */
672 write
? SD_WRITE_MULTIPLE_BLOCK
: SD_READ_MULTIPLE_BLOCK
;
673 unsigned long bank_start
= start
;
675 /* Only switch banks for internal storage */
676 if(drive
== INTERNAL_AS3525
)
678 unsigned int bank
= start
/ BLOCKS_PER_BANK
; /* Current bank */
680 /* Switch bank if needed */
681 if(card_info
[INTERNAL_AS3525
].current_bank
!= bank
)
683 ret
= sd_select_bank(bank
);
687 goto sd_transfer_error
;
691 /* Adjust start block in current bank */
692 bank_start
-= bank
* BLOCKS_PER_BANK
;
694 /* Do not cross a bank boundary in a single transfer loop */
695 if((transfer
+ bank_start
) > BLOCKS_PER_BANK
)
696 transfer
= BLOCKS_PER_BANK
- bank_start
;
699 dma_buf
= aligned_buffer
;
700 if(transfer
> UNALIGNED_NUM_SECTORS
)
701 transfer
= UNALIGNED_NUM_SECTORS
;
703 memcpy(uncached_buffer
, buf
, transfer
* SD_BLOCK_SIZE
);
705 /* Set bank_start to the correct unit (blocks or bytes) */
706 if(!(card_info
[drive
].ocr
& (1<<30))) /* not SDHC */
707 bank_start
*= SD_BLOCK_SIZE
;
709 ret
= sd_wait_for_state(drive
, SD_TRAN
);
713 goto sd_transfer_error
;
716 if(!send_cmd(drive
, cmd
, bank_start
, MCI_ARG
, NULL
))
719 goto sd_transfer_error
;
723 dma_enable_channel(0, dma_buf
, MCI_FIFO(drive
),
724 (drive
== INTERNAL_AS3525
) ? DMA_PERI_SD
: DMA_PERI_SD_SLOT
,
725 DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
, NULL
);
727 dma_enable_channel(0, MCI_FIFO(drive
), dma_buf
,
728 (drive
== INTERNAL_AS3525
) ? DMA_PERI_SD
: DMA_PERI_SD_SLOT
,
729 DMAC_FLOWCTRL_PERI_PERI_TO_MEM
, false, true, 0, DMA_S8
, NULL
);
731 /* FIXME : we should check if the timeouts calculated from the card's
732 * CSD are lower, and use them if it is the case
733 * Note : the OF doesn't seem to use them anyway */
734 MCI_DATA_TIMER(drive
) = write
?
735 SD_MAX_WRITE_TIMEOUT
: SD_MAX_READ_TIMEOUT
;
736 MCI_DATA_LENGTH(drive
) = transfer
* SD_BLOCK_SIZE
;
737 MCI_DATA_CTRL(drive
) = (1<<0) /* enable */ |
738 (!write
<<1) /* transfer direction */ |
740 (9<<4) /* 2^9 = 512 */ ;
742 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
744 /* Wait for FIFO to empty, card may still be in PRG state for writes */
745 while(MCI_STATUS(drive
) & MCI_TX_ACTIVE
);
747 last_disk_activity
= current_tick
;
749 if(!send_cmd(drive
, SD_STOP_TRANSMISSION
, 0, MCI_NO_FLAGS
, NULL
))
752 goto sd_transfer_error
;
755 if(!transfer_error
[drive
])
758 memcpy(buf
, uncached_buffer
, transfer
* SD_BLOCK_SIZE
);
759 buf
+= transfer
* SD_BLOCK_SIZE
;
762 loops
= 0; /* reset errors counter */
764 else if(loops
++ > PL180_MAX_TRANSFER_ERRORS
)
765 panicf("SD Xfer %s err:0x%x Disk%d", (write
? "write": "read"),
766 transfer_error
[drive
], drive
);
769 ret
= 0; /* success */
781 card_info
[drive
].initialized
= 0;
783 mutex_unlock(&sd_mtx
);
787 int sd_read_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
790 return sd_transfer_sectors(IF_MD2(drive
,) start
, count
, buf
, false);
793 int sd_write_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
797 #ifdef BOOTLOADER /* we don't need write support in bootloader */
798 #ifdef HAVE_MULTIDRIVE
806 return sd_transfer_sectors(IF_MD2(drive
,) start
, count
, (void*)buf
, true);
811 long sd_last_disk_activity(void)
813 return last_disk_activity
;
816 void sd_enable(bool on
)
818 #if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE)
819 extern int buttonlight_is_on
;
823 static bool cpu_boosted
= false;
826 if (sd_enabled
== on
)
827 return; /* nothing to do */
830 /* Enable both NAF_CLOCK & IDE clk for internal SD */
831 CGU_PERI
|= CGU_NAF_CLOCK_ENABLE
;
832 CGU_IDE
|= (1<<6); /* enable non AHB interface*/
833 #ifdef HAVE_MULTIDRIVE
834 /* Enable MCI clk for uSD */
835 CGU_PERI
|= CGU_MCI_CLOCK_ENABLE
;
836 #ifdef HAVE_BUTTON_LIGHT
837 /* buttonlight AMSes need a bit of special handling for the buttonlight
838 * here due to the dual mapping of GPIOD and XPD */
839 CCU_IO
|= (1<<2); /* XPD is SD-MCI interface (b3:2 = 01) */
840 if (buttonlight_is_on
)
841 GPIOD_DIR
&= ~(1<<7);
844 #endif /* HAVE_BUTTON_LIGHT */
845 #endif /* HAVE_MULTIDRIVE */
849 if(card_detect_target()) /* If SD card present Boost cpu for voltage */
867 #ifdef HAVE_MULTIDRIVE
868 #ifdef HAVE_BUTTON_LIGHT
869 CCU_IO
&= ~(1<<2); /* XPD is general purpose IO (b3:2 = 00) */
870 if (buttonlight_is_on
)
872 #endif /* HAVE_BUTTON_LIGHT */
873 /* Disable MCI clk for uSD */
874 CGU_PERI
&= ~CGU_MCI_CLOCK_ENABLE
;
875 #endif /* HAVE_MULTIDRIVE */
877 /* Disable both NAF_CLOCK & IDE clk for internal SD */
878 CGU_PERI
&= ~CGU_NAF_CLOCK_ENABLE
;
879 CGU_IDE
&= ~(1<<6); /* disable non AHB interface*/
883 tCardInfo
*card_get_info_target(int card_no
)
885 return &card_info
[card_no
];
889 void card_enable_monitoring_target(bool on
)
891 if (on
) /* enable interrupt */
893 else /* disable interrupt */
896 #endif /* HAVE_HOTSWAP */
898 #endif /* !BOOTLOADER */
900 #ifdef CONFIG_STORAGE_MULTI
901 int sd_num_drives(int first_drive
)
903 /* We don't care which logical drive number(s) we have been assigned */
908 #endif /* CONFIG_STORAGE_MULTI */