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 #include "config.h" /* for HAVE_MULTIDRIVE & AMS_OF_SIZE */
36 #include "pl180.h" /* SD controller */
37 #include "pl081.h" /* DMA controller */
38 #include "dma-target.h" /* DMA request lines */
39 #include "clock-target.h"
41 #ifdef HAVE_BUTTON_LIGHT
42 #include "backlight-target.h"
45 #include "ata_idle_notify.h"
54 #define MCI_NO_FLAGS (0<<0)
55 #define MCI_RESP (1<<0)
56 #define MCI_LONG_RESP (1<<1)
57 #define MCI_ARG (1<<2)
59 /* ARM PL180 registers */
60 #define MCI_POWER(i) (*(volatile unsigned char *) (pl180_base[i]+0x00))
61 #define MCI_CLOCK(i) (*(volatile unsigned long *) (pl180_base[i]+0x04))
62 #define MCI_ARGUMENT(i) (*(volatile unsigned long *) (pl180_base[i]+0x08))
63 #define MCI_COMMAND(i) (*(volatile unsigned long *) (pl180_base[i]+0x0C))
64 #define MCI_RESPCMD(i) (*(volatile unsigned long *) (pl180_base[i]+0x10))
65 #define MCI_RESP0(i) (*(volatile unsigned long *) (pl180_base[i]+0x14))
66 #define MCI_RESP1(i) (*(volatile unsigned long *) (pl180_base[i]+0x18))
67 #define MCI_RESP2(i) (*(volatile unsigned long *) (pl180_base[i]+0x1C))
68 #define MCI_RESP3(i) (*(volatile unsigned long *) (pl180_base[i]+0x20))
69 #define MCI_DATA_TIMER(i) (*(volatile unsigned long *) (pl180_base[i]+0x24))
70 #define MCI_DATA_LENGTH(i) (*(volatile unsigned short*) (pl180_base[i]+0x28))
71 #define MCI_DATA_CTRL(i) (*(volatile unsigned char *) (pl180_base[i]+0x2C))
72 #define MCI_DATA_CNT(i) (*(volatile unsigned short*) (pl180_base[i]+0x30))
73 #define MCI_STATUS(i) (*(volatile unsigned long *) (pl180_base[i]+0x34))
74 #define MCI_CLEAR(i) (*(volatile unsigned long *) (pl180_base[i]+0x38))
75 #define MCI_MASK0(i) (*(volatile unsigned long *) (pl180_base[i]+0x3C))
76 #define MCI_MASK1(i) (*(volatile unsigned long *) (pl180_base[i]+0x40))
77 #define MCI_SELECT(i) (*(volatile unsigned long *) (pl180_base[i]+0x44))
78 #define MCI_FIFO_CNT(i) (*(volatile unsigned long *) (pl180_base[i]+0x48))
80 #define MCI_DATA_ERROR \
87 #define MCI_RESPONSE_ERROR \
91 #define MCI_FIFO(i) ((unsigned long *) (pl180_base[i]+0x80))
93 #define INTERNAL_AS3525 0 /* embedded SD card */
94 #define SD_SLOT_AS3525 1 /* SD slot if present */
96 static const int pl180_base
[NUM_DRIVES
] = {
98 #ifdef HAVE_MULTIDRIVE
103 static int sd_wait_for_state(const int drive
, unsigned int state
);
104 static int sd_select_bank(signed char bank
);
105 static int sd_init_card(const int drive
);
106 static void init_pl180_controller(const int drive
);
108 #define BLOCKS_PER_BANK 0x7a7800u
110 static tCardInfo card_info
[NUM_DRIVES
];
112 /* maximum timeouts recommanded in the SD Specification v2.00 */
113 #define SD_MAX_READ_TIMEOUT ((AS3525_PCLK_FREQ) / 1000 * 100) /* 100 ms */
114 #define SD_MAX_WRITE_TIMEOUT ((AS3525_PCLK_FREQ) / 1000 * 250) /* 250 ms */
116 /* for compatibility */
117 static long last_disk_activity
= -1;
119 #define MIN_YIELD_PERIOD 5 /* ticks */
120 static long next_yield
= 0;
122 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x200)/sizeof(long)];
123 static const char sd_thread_name
[] = "ata/sd";
124 static struct mutex sd_mtx
;
125 static struct event_queue sd_queue
;
127 bool sd_enabled
= false;
130 #if defined(HAVE_MULTIDRIVE)
131 static bool hs_card
= false;
132 #define EXT_SD_BITS (1<<2)
135 static struct wakeup transfer_completion_signal
;
136 static volatile unsigned int transfer_error
[NUM_VOLUMES
];
137 #define PL180_MAX_TRANSFER_ERRORS 10
139 #define UNALIGNED_NUM_SECTORS 10
140 static unsigned char aligned_buffer
[UNALIGNED_NUM_SECTORS
* SD_BLOCK_SIZE
] __attribute__((aligned(32))); /* align on cache line size */
141 static unsigned char *uncached_buffer
= UNCACHED_ADDR(&aligned_buffer
[0]);
144 static inline void mci_delay(void) { udelay(1000) ; }
147 static inline bool card_detect_target(void)
149 #if defined(HAVE_MULTIDRIVE)
150 return !(GPIOA_PIN(2));
158 static int sd1_oneshot_callback(struct timeout
*tmo
)
162 /* This is called only if the state was stable for 300ms - check state
163 * and post appropriate event. */
164 if (card_detect_target())
166 queue_broadcast(SYS_HOTSWAP_INSERTED
, 0);
169 queue_broadcast(SYS_HOTSWAP_EXTRACTED
, 0);
174 void sd_gpioa_isr(void)
176 static struct timeout sd1_oneshot
;
177 if (GPIOA_MIS
& EXT_SD_BITS
)
178 timeout_register(&sd1_oneshot
, sd1_oneshot_callback
, (3*HZ
/10), 0);
179 /* acknowledge interrupt */
180 GPIOA_IC
= EXT_SD_BITS
;
182 #endif /* HAVE_HOTSWAP */
186 const int status
= MCI_STATUS(INTERNAL_AS3525
);
188 transfer_error
[INTERNAL_AS3525
] = status
& MCI_DATA_ERROR
;
190 wakeup_signal(&transfer_completion_signal
);
191 MCI_CLEAR(INTERNAL_AS3525
) = status
;
194 #ifdef HAVE_MULTIDRIVE
197 const int status
= MCI_STATUS(SD_SLOT_AS3525
);
199 transfer_error
[SD_SLOT_AS3525
] = status
& MCI_DATA_ERROR
;
201 wakeup_signal(&transfer_completion_signal
);
202 MCI_CLEAR(SD_SLOT_AS3525
) = status
;
206 static bool send_cmd(const int drive
, const int cmd
, const int arg
,
207 const int flags
, long *response
)
211 /* Clear old status flags */
212 MCI_CLEAR(drive
) = 0x7ff;
214 /* Load command argument or clear if none */
215 MCI_ARGUMENT(drive
) = (flags
& MCI_ARG
) ? arg
: 0;
217 /* Construct MCI_COMMAND & enable CPSM */
220 /* b6 */| ((flags
& (MCI_RESP
|MCI_LONG_RESP
)) ? MCI_COMMAND_RESPONSE
: 0)
221 /* b7 */| ((flags
& MCI_LONG_RESP
) ? MCI_COMMAND_LONG_RESPONSE
: 0)
222 /* b8 | MCI_COMMAND_INTERRUPT */
223 /* b9 | MCI_COMMAND_PENDING */ /*Only used with stream data transfer*/
224 /* b10*/| MCI_COMMAND_ENABLE
; /* Enables CPSM */
226 /* Wait while cmd completes then disable CPSM */
227 while(MCI_STATUS(drive
) & MCI_CMD_ACTIVE
);
228 MCI_COMMAND(drive
) = 0;
230 status
= MCI_STATUS(drive
);
232 /* Handle command responses */
233 if(flags
& MCI_RESP
) /* CMD expects response */
235 response
[0] = MCI_RESP0(drive
); /* Always prepare short response */
237 if(status
& MCI_RESPONSE_ERROR
) /* timeout or crc failure */
240 if(status
& MCI_CMD_RESP_END
) /* Response passed CRC check */
242 if(flags
& MCI_LONG_RESP
)
243 { /* response[0] has already been read */
244 response
[1] = MCI_RESP1(drive
);
245 response
[2] = MCI_RESP2(drive
);
246 response
[3] = MCI_RESP3(drive
);
251 else if(status
& MCI_CMD_SENT
) /* CMD sent, no response required */
257 #define MCI_FULLSPEED (MCI_CLOCK_ENABLE | MCI_CLOCK_BYPASS) /* MCLK */
258 #define MCI_HALFSPEED (MCI_CLOCK_ENABLE) /* MCLK/2 */
259 #define MCI_QUARTERSPEED (MCI_CLOCK_ENABLE | 1) /* MCLK/4 */
260 #define MCI_IDENTSPEED (MCI_CLOCK_ENABLE | AS3525_SD_IDENT_DIV) /* IDENT */
262 static int sd_init_card(const int drive
)
264 unsigned long response
;
268 /* MCLCK on and set to 400kHz ident frequency */
269 MCI_CLOCK(drive
) = MCI_IDENTSPEED
;
271 /* 100 - 400kHz clock required for Identification Mode */
272 /* Start of Card Identification Mode ************************************/
275 if(!send_cmd(drive
, SD_GO_IDLE_STATE
, 0, MCI_NO_FLAGS
, NULL
))
279 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
280 Non v2 cards will not respond to this command*/
281 if(send_cmd(drive
, SD_SEND_IF_COND
, 0x1AA, MCI_RESP
|MCI_ARG
, &response
))
282 if((response
& 0xFFF) == 0x1AA)
285 /* timeout for initialization is 1sec, from SD Specification 2.00 */
286 init_timeout
= current_tick
+ HZ
;
289 /* this timeout is the only valid error for this loop*/
290 if(TIME_AFTER(current_tick
, init_timeout
))
294 send_cmd(drive
, SD_APP_CMD
, 0, MCI_RESP
|MCI_ARG
, &response
);
296 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
297 send_cmd(drive
, SD_APP_OP_COND
, (0x00FF8000 | (sd_v2
? 1<<30 : 0)),
298 MCI_RESP
|MCI_ARG
, &card_info
[drive
].ocr
);
300 } while(!(card_info
[drive
].ocr
& (1<<31)));
303 if(!send_cmd(drive
, SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
|MCI_ARG
,
304 card_info
[drive
].cid
))
308 if(!send_cmd(drive
, SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
|MCI_ARG
,
309 &card_info
[drive
].rca
))
312 /* End of Card Identification Mode ************************************/
314 #ifdef HAVE_MULTIDRIVE /* The internal SDs are v1 */
316 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
319 /* CMD7 w/rca: Select card to put it in TRAN state */
320 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_ARG
, NULL
))
324 if(sd_wait_for_state(drive
, SD_TRAN
))
327 if(!send_cmd(drive
, SD_SWITCH_FUNC
, 0x80fffff1, MCI_ARG
, NULL
))
331 /* go back to STBY state so we can read csd */
332 /* CMD7 w/rca=0: Deselect card to put it in STBY state */
333 if(!send_cmd(drive
, SD_DESELECT_CARD
, 0, MCI_ARG
, NULL
))
337 #endif /* HAVE_MULTIDRIVE */
340 if(!send_cmd(drive
, SD_SEND_CSD
, card_info
[drive
].rca
,
341 MCI_RESP
|MCI_LONG_RESP
|MCI_ARG
, card_info
[drive
].csd
))
344 sd_parse_csd(&card_info
[drive
]);
346 #if defined(HAVE_MULTIDRIVE)
347 hs_card
= (card_info
[drive
].speed
== 50000000);
350 /* Boost MCICLK to operating speed */
351 if(drive
== INTERNAL_AS3525
)
352 MCI_CLOCK(drive
) = MCI_HALFSPEED
; /* MCICLK = IDE_CLK/2 = 25 MHz */
353 #if defined(HAVE_MULTIDRIVE)
355 /* MCICLK = PCLK/2 = 31MHz(HS) or PCLK/4 = 15.5 Mhz (STD)*/
356 MCI_CLOCK(drive
) = (hs_card
? MCI_HALFSPEED
: MCI_QUARTERSPEED
);
359 /* CMD7 w/rca: Select card to put it in TRAN state */
360 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_ARG
, NULL
))
365 * enable bank switching
366 * without issuing this command, we only have access to 1/4 of the blocks
367 * of the first bank (0x1E9E00 blocks, which is the size reported in the
370 if(drive
== INTERNAL_AS3525
)
372 const int ret
= sd_select_bank(-1);
376 /* CMD7 w/rca = 0: Select card to put it in STBY state */
377 if(!send_cmd(drive
, SD_SELECT_CARD
, 0, MCI_ARG
, NULL
))
381 /* CMD9 send CSD again, so we got the correct number of blocks */
382 if(!send_cmd(drive
, SD_SEND_CSD
, card_info
[drive
].rca
,
383 MCI_RESP
|MCI_LONG_RESP
|MCI_ARG
, card_info
[drive
].csd
))
386 sd_parse_csd(&card_info
[drive
]);
387 /* The OF is stored in the first blocks */
388 card_info
[INTERNAL_AS3525
].numblocks
-= AMS_OF_SIZE
;
390 /* CMD7 w/rca: Select card to put it in TRAN state */
391 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_ARG
, NULL
))
396 card_info
[drive
].initialized
= 1;
401 static void sd_thread(void) __attribute__((noreturn
));
402 static void sd_thread(void)
404 struct queue_event ev
;
405 bool idle_notified
= false;
409 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
414 case SYS_HOTSWAP_INSERTED
:
415 case SYS_HOTSWAP_EXTRACTED
:
417 int microsd_init
= 1;
418 fat_lock(); /* lock-out FAT activity first -
419 prevent deadlocking via disk_mount that
420 would cause a reverse-order attempt with
422 mutex_lock(&sd_mtx
); /* lock-out card activity - direct calls
423 into driver that bypass the fat cache */
425 /* We now have exclusive control of fat cache and ata */
427 disk_unmount(SD_SLOT_AS3525
); /* release "by force", ensure file
428 descriptors aren't leaked and any busy
429 ones are invalid if mounting */
431 /* Force card init for new card, re-init for re-inserted one or
432 * clear if the last attempt to init failed with an error. */
433 card_info
[SD_SLOT_AS3525
].initialized
= 0;
435 if (ev
.id
== SYS_HOTSWAP_INSERTED
)
438 init_pl180_controller(SD_SLOT_AS3525
);
439 microsd_init
= sd_init_card(SD_SLOT_AS3525
);
440 if (microsd_init
< 0) /* initialisation failed */
441 panicf("microSD init failed : %d", microsd_init
);
443 microsd_init
= disk_mount(SD_SLOT_AS3525
); /* 0 if fail */
447 * Mount succeeded, or this was an EXTRACTED event,
448 * in both cases notify the system about the changed filesystems
451 queue_broadcast(SYS_FS_CHANGED
, 0);
453 /* Access is now safe */
454 mutex_unlock(&sd_mtx
);
461 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
463 idle_notified
= false;
467 /* never let a timer wrap confuse us */
468 next_yield
= current_tick
;
472 call_storage_idle_notifys(false);
473 idle_notified
= true;
478 case SYS_USB_CONNECTED
:
479 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
480 /* Wait until the USB cable is extracted again */
481 usb_wait_for_disconnect(&sd_queue
);
484 case SYS_USB_DISCONNECTED
:
485 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
491 static void init_pl180_controller(const int drive
)
493 MCI_COMMAND(drive
) = MCI_DATA_CTRL(drive
) = 0;
494 MCI_CLEAR(drive
) = 0x7ff;
496 MCI_MASK0(drive
) = MCI_DATA_ERROR
| MCI_DATA_END
;
497 MCI_MASK1(drive
) = 0;
498 #ifdef HAVE_MULTIDRIVE
500 (drive
== INTERNAL_AS3525
) ? INTERRUPT_NAND
: INTERRUPT_MCI0
;
501 /* clear previous irq */
502 GPIOA_IC
= EXT_SD_BITS
;
503 /* enable edge detecting */
504 GPIOA_IS
&= ~EXT_SD_BITS
;
505 /* detect both raising and falling edges */
506 GPIOA_IBE
|= EXT_SD_BITS
;
509 VIC_INT_ENABLE
= INTERRUPT_NAND
;
512 MCI_POWER(drive
) = MCI_POWER_UP
| (MCI_VDD_3_0
); /* OF Setting */
515 MCI_POWER(drive
) |= MCI_POWER_ON
;
518 MCI_SELECT(drive
) = 0;
520 /* Pl180 clocks get turned on at start of card init */
526 CGU_IDE
= (1<<6) /* enable non AHB interface*/
527 | (AS3525_IDE_DIV
<< 2)
528 | AS3525_CLK_PLLA
; /* clock source = PLLA */
530 CGU_PERI
|= CGU_NAF_CLOCK_ENABLE
;
531 #ifdef HAVE_MULTIDRIVE
532 CGU_PERI
|= CGU_MCI_CLOCK_ENABLE
;
533 CCU_IO
&= ~(1<<3); /* bits 3:2 = 01, xpd is SD interface */
537 wakeup_init(&transfer_completion_signal
);
539 init_pl180_controller(INTERNAL_AS3525
);
540 ret
= sd_init_card(INTERNAL_AS3525
);
543 #ifdef HAVE_MULTIDRIVE
544 init_pl180_controller(SD_SLOT_AS3525
);
550 queue_init(&sd_queue
, true);
551 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
552 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
) IF_COP(, CPU
));
562 bool sd_removable(IF_MD_NONVOID(int drive
))
567 bool sd_present(IF_MD_NONVOID(int drive
))
569 return (drive
== 0) ? true : card_detect_target();
571 #endif /* HAVE_HOTSWAP */
573 static int sd_wait_for_state(const int drive
, unsigned int state
)
575 unsigned long response
= 0;
576 unsigned int timeout
= current_tick
+ HZ
;
580 if(!send_cmd(drive
, SD_SEND_STATUS
, card_info
[drive
].rca
,
581 MCI_RESP
|MCI_ARG
, &response
))
584 if (((response
>> 9) & 0xf) == state
)
587 if(TIME_AFTER(current_tick
, timeout
))
590 if (TIME_AFTER(current_tick
, next_yield
))
593 next_yield
= current_tick
+ MIN_YIELD_PERIOD
;
598 static int sd_select_bank(signed char bank
)
604 if(loops
++ > PL180_MAX_TRANSFER_ERRORS
)
605 panicf("SD bank %d error : 0x%x", bank
,
606 transfer_error
[INTERNAL_AS3525
]);
608 ret
= sd_wait_for_state(INTERNAL_AS3525
, SD_TRAN
);
612 if(!send_cmd(INTERNAL_AS3525
, SD_SWITCH_FUNC
, 0x80ffffef, MCI_ARG
, NULL
))
617 if(!send_cmd(INTERNAL_AS3525
, 35, 0, MCI_NO_FLAGS
, NULL
))
622 memset(uncached_buffer
, 0, 512);
624 { /* enable bank switching */
625 uncached_buffer
[0] = 16;
626 uncached_buffer
[1] = 1;
627 uncached_buffer
[2] = 10;
630 uncached_buffer
[0] = bank
;
633 /* we don't use the uncached buffer here, because we need the
634 * physical memory address for DMA transfers */
635 dma_enable_channel(0, aligned_buffer
, MCI_FIFO(INTERNAL_AS3525
),
636 DMA_PERI_SD
, DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
,
639 MCI_DATA_TIMER(INTERNAL_AS3525
) = SD_MAX_WRITE_TIMEOUT
;
640 MCI_DATA_LENGTH(INTERNAL_AS3525
) = 512;
641 MCI_DATA_CTRL(INTERNAL_AS3525
) = (1<<0) /* enable */ |
642 (0<<1) /* transfer direction */ |
644 (9<<4) /* 2^9 = 512 */ ;
646 /* Wakeup signal from NAND/MCIO isr on MCI_DATA_ERROR | MCI_DATA_END */
647 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
649 /* Wait for FIFO to empty, card may still be in PRG state */
650 while(MCI_STATUS(INTERNAL_AS3525
) & MCI_TX_ACTIVE
);
654 } while(transfer_error
[INTERNAL_AS3525
]);
656 card_info
[INTERNAL_AS3525
].current_bank
= (bank
== -1) ? 0 : bank
;
661 static int sd_transfer_sectors(IF_MD2(int drive
,) unsigned long start
,
662 int count
, void* buf
, const bool write
)
664 #ifndef HAVE_MULTIDRIVE
676 if (card_info
[drive
].initialized
<= 0)
678 ret
= sd_init_card(drive
);
679 if (!(card_info
[drive
].initialized
))
680 goto sd_transfer_error
;
683 if(count
< 0) /* XXX: why is it signed ? */
686 goto sd_transfer_error
;
688 if((start
+count
) > card_info
[drive
].numblocks
)
691 goto sd_transfer_error
;
694 /* skip SanDisk OF */
695 if (drive
== INTERNAL_AS3525
)
696 start
+= AMS_OF_SIZE
;
698 last_disk_activity
= current_tick
;
704 /* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH
705 * register, so we have to transfer maximum 127 sectors at a time. */
706 unsigned int transfer
= (count
>= 128) ? 127 : count
; /* sectors */
709 write
? SD_WRITE_MULTIPLE_BLOCK
: SD_READ_MULTIPLE_BLOCK
;
710 unsigned long bank_start
= start
;
712 /* Only switch banks for internal storage */
713 if(drive
== INTERNAL_AS3525
)
715 unsigned int bank
= 0;
716 while(bank_start
>= BLOCKS_PER_BANK
)
718 bank_start
-= BLOCKS_PER_BANK
;
722 /* Switch bank if needed */
723 if(card_info
[INTERNAL_AS3525
].current_bank
!= bank
)
725 ret
= sd_select_bank(bank
);
729 goto sd_transfer_error
;
733 /* Do not cross a bank boundary in a single transfer loop */
734 if((transfer
+ bank_start
) > BLOCKS_PER_BANK
)
735 transfer
= BLOCKS_PER_BANK
- bank_start
;
738 /* Set bank_start to the correct unit (blocks or bytes) */
739 if(!(card_info
[drive
].ocr
& (1<<30))) /* not SDHC */
740 bank_start
*= SD_BLOCK_SIZE
;
742 dma_buf
= aligned_buffer
;
743 if(transfer
> UNALIGNED_NUM_SECTORS
)
744 transfer
= UNALIGNED_NUM_SECTORS
;
747 memcpy(uncached_buffer
, buf
, transfer
* SD_BLOCK_SIZE
);
749 ret
= sd_wait_for_state(drive
, SD_TRAN
);
753 goto sd_transfer_error
;
756 if(!send_cmd(drive
, cmd
, bank_start
, MCI_ARG
, NULL
))
759 goto sd_transfer_error
;
764 dma_enable_channel(0, dma_buf
, MCI_FIFO(drive
),
765 (drive
== INTERNAL_AS3525
) ? DMA_PERI_SD
: DMA_PERI_SD_SLOT
,
766 DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
, NULL
);
768 /*Small delay for writes prevents data crc failures at lower freqs*/
769 #ifdef HAVE_MULTIDRIVE
770 if((drive
== SD_SLOT_AS3525
) && !hs_card
)
772 int write_delay
= 125;
773 while(write_delay
--);
778 dma_enable_channel(0, MCI_FIFO(drive
), dma_buf
,
779 (drive
== INTERNAL_AS3525
) ? DMA_PERI_SD
: DMA_PERI_SD_SLOT
,
780 DMAC_FLOWCTRL_PERI_PERI_TO_MEM
, false, true, 0, DMA_S8
, NULL
);
782 MCI_DATA_TIMER(drive
) = write
?
783 SD_MAX_WRITE_TIMEOUT
: SD_MAX_READ_TIMEOUT
;
784 MCI_DATA_LENGTH(drive
) = transfer
* SD_BLOCK_SIZE
;
785 MCI_DATA_CTRL(drive
) = (1<<0) /* enable */ |
786 (!write
<<1) /* transfer direction */ |
788 (9<<4) /* 2^9 = 512 */ ;
790 /* Wakeup signal from NAND/MCIO isr on MCI_DATA_ERROR | MCI_DATA_END */
791 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
793 /* Wait for FIFO to empty, card may still be in PRG state for writes */
794 while(MCI_STATUS(drive
) & MCI_TX_ACTIVE
);
796 last_disk_activity
= current_tick
;
798 if(!send_cmd(drive
, SD_STOP_TRANSMISSION
, 0, MCI_NO_FLAGS
, NULL
))
801 goto sd_transfer_error
;
804 if(!transfer_error
[drive
])
807 memcpy(buf
, uncached_buffer
, transfer
* SD_BLOCK_SIZE
);
808 buf
+= transfer
* SD_BLOCK_SIZE
;
811 loops
= 0; /* reset errors counter */
813 else if(loops
++ > PL180_MAX_TRANSFER_ERRORS
)
814 panicf("SD Xfer %s err:0x%x Disk%d", (write
? "write": "read"),
815 transfer_error
[drive
], drive
);
818 ret
= 0; /* success */
830 card_info
[drive
].initialized
= 0;
832 mutex_unlock(&sd_mtx
);
836 int sd_read_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
839 return sd_transfer_sectors(IF_MD2(drive
,) start
, count
, buf
, false);
842 int sd_write_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
846 #ifdef BOOTLOADER /* we don't need write support in bootloader */
847 #ifdef HAVE_MULTIDRIVE
855 return sd_transfer_sectors(IF_MD2(drive
,) start
, count
, (void*)buf
, true);
860 long sd_last_disk_activity(void)
862 return last_disk_activity
;
865 void sd_enable(bool on
)
867 #if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE)
868 extern int buttonlight_is_on
;
871 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
872 static bool cpu_boosted
= false;
875 if (sd_enabled
== on
)
876 return; /* nothing to do */
879 /* Enable both NAF_CLOCK & IDE clk for internal SD */
880 CGU_PERI
|= CGU_NAF_CLOCK_ENABLE
;
881 CGU_IDE
|= (1<<6); /* enable non AHB interface*/
882 #ifdef HAVE_MULTIDRIVE
883 /* Enable MCI clk for uSD */
884 CGU_PERI
|= CGU_MCI_CLOCK_ENABLE
;
885 #ifdef HAVE_BUTTON_LIGHT
886 /* buttonlight AMSes need a bit of special handling for the buttonlight
887 * here due to the dual mapping of GPIOD and XPD */
888 CCU_IO
|= (1<<2); /* XPD is SD-MCI interface (b3:2 = 01) */
889 if (buttonlight_is_on
)
890 GPIOD_DIR
&= ~(1<<7);
893 #endif /* HAVE_BUTTON_LIGHT */
894 #endif /* HAVE_MULTIDRIVE */
897 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
898 if(card_detect_target()) /* If SD card present Boost cpu for voltage */
903 #endif /* defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE) */
907 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
913 #endif /* defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE) */
915 /* not sure why we have to wait, but without this, test_disk freezes
916 * when closing the 300MB file which was just written to */
921 #ifdef HAVE_MULTIDRIVE
922 #ifdef HAVE_BUTTON_LIGHT
923 CCU_IO
&= ~(1<<2); /* XPD is general purpose IO (b3:2 = 00) */
924 if (buttonlight_is_on
)
926 #endif /* HAVE_BUTTON_LIGHT */
927 /* Disable MCI clk for uSD */
928 CGU_PERI
&= ~CGU_MCI_CLOCK_ENABLE
;
929 #endif /* HAVE_MULTIDRIVE */
931 /* Disable both NAF_CLOCK & IDE clk for internal SD */
932 CGU_PERI
&= ~CGU_NAF_CLOCK_ENABLE
;
933 CGU_IDE
&= ~(1<<6); /* disable non AHB interface*/
937 tCardInfo
*card_get_info_target(int card_no
)
939 return &card_info
[card_no
];
943 void card_enable_monitoring_target(bool on
)
945 if (on
) /* enable interrupt */
946 GPIOA_IE
|= EXT_SD_BITS
;
947 else /* disable interrupt */
948 GPIOA_IE
&= ~EXT_SD_BITS
;
950 #endif /* HAVE_HOTSWAP */
952 #endif /* !BOOTLOADER */
954 #ifdef CONFIG_STORAGE_MULTI
955 int sd_num_drives(int first_drive
)
957 /* We don't care which logical drive number(s) we have been assigned */
962 #endif /* CONFIG_STORAGE_MULTI */