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_tran_state(const int drive
);
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
= AS3525_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 unsigned cmd_retries
= 6;
214 /* Clear old status flags */
215 MCI_CLEAR(drive
) = 0x7ff;
217 /* Load command argument or clear if none */
218 MCI_ARGUMENT(drive
) = (flags
& MCI_ARG
) ? arg
: 0;
220 /* Construct MCI_COMMAND & enable CPSM */
223 /* b6 */| ((flags
& (MCI_RESP
|MCI_LONG_RESP
)) ? MCI_COMMAND_RESPONSE
: 0)
224 /* b7 */| ((flags
& MCI_LONG_RESP
) ? MCI_COMMAND_LONG_RESPONSE
: 0)
225 /* b8 | MCI_COMMAND_INTERRUPT */
226 /* b9 | MCI_COMMAND_PENDING */ /*Only used with stream data transfer*/
227 /* b10*/| MCI_COMMAND_ENABLE
; /* Enables CPSM */
229 /* Wait while cmd completes then disable CPSM */
230 while(MCI_STATUS(drive
) & MCI_CMD_ACTIVE
);
231 MCI_COMMAND(drive
) = 0;
233 status
= MCI_STATUS(drive
);
235 /* Handle command responses */
236 if(flags
& MCI_RESP
) /* CMD expects response */
238 response
[0] = MCI_RESP0(drive
); /* Always prepare short response */
240 if(status
& MCI_RESPONSE_ERROR
) /* timeout or crc failure */
243 if(status
& MCI_CMD_RESP_END
) /* Response passed CRC check */
245 if(flags
& MCI_LONG_RESP
)
246 { /* response[0] has already been read */
247 response
[1] = MCI_RESP1(drive
);
248 response
[2] = MCI_RESP2(drive
);
249 response
[3] = MCI_RESP3(drive
);
254 else if(status
& MCI_CMD_SENT
) /* CMD sent, no response required */
261 #define MCI_FULLSPEED (MCI_CLOCK_ENABLE | MCI_CLOCK_BYPASS) /* MCLK */
262 #define MCI_HALFSPEED (MCI_CLOCK_ENABLE) /* MCLK/2 */
263 #define MCI_QUARTERSPEED (MCI_CLOCK_ENABLE | 1) /* MCLK/4 */
264 #define MCI_IDENTSPEED (MCI_CLOCK_ENABLE | AS3525_SD_IDENT_DIV) /* IDENT */
266 static int sd_init_card(const int drive
)
268 unsigned long response
;
272 /* MCLCK on and set to 400kHz ident frequency */
273 MCI_CLOCK(drive
) = MCI_IDENTSPEED
;
275 /* 100 - 400kHz clock required for Identification Mode */
276 /* Start of Card Identification Mode ************************************/
279 if(!send_cmd(drive
, SD_GO_IDLE_STATE
, 0, MCI_NO_FLAGS
, NULL
))
283 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
284 Non v2 cards will not respond to this command*/
285 if(send_cmd(drive
, SD_SEND_IF_COND
, 0x1AA, MCI_RESP
|MCI_ARG
, &response
))
286 if((response
& 0xFFF) == 0x1AA)
289 /* timeout for initialization is 1sec, from SD Specification 2.00 */
290 init_timeout
= current_tick
+ HZ
;
293 /* this timeout is the only valid error for this loop*/
294 if(TIME_AFTER(current_tick
, init_timeout
))
298 send_cmd(drive
, SD_APP_CMD
, 0, MCI_RESP
|MCI_ARG
, &response
);
300 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
301 send_cmd(drive
, SD_APP_OP_COND
, (0x00FF8000 | (sd_v2
? 1<<30 : 0)),
302 MCI_RESP
|MCI_ARG
, &card_info
[drive
].ocr
);
304 } while(!(card_info
[drive
].ocr
& (1<<31)));
307 if(!send_cmd(drive
, SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
|MCI_ARG
,
308 card_info
[drive
].cid
))
312 if(!send_cmd(drive
, SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
|MCI_ARG
,
313 &card_info
[drive
].rca
))
316 /* End of Card Identification Mode ************************************/
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
))
328 if(sd_wait_for_tran_state(drive
))
331 if(!send_cmd(drive
, SD_SWITCH_FUNC
, 0x80fffff1, MCI_ARG
, NULL
))
335 /* go back to STBY state so we can read csd */
336 /* CMD7 w/rca=0: Deselect card to put it in STBY state */
337 if(!send_cmd(drive
, SD_DESELECT_CARD
, 0, MCI_ARG
, NULL
))
341 #endif /* HAVE_MULTIDRIVE */
344 if(!send_cmd(drive
, SD_SEND_CSD
, card_info
[drive
].rca
,
345 MCI_RESP
|MCI_LONG_RESP
|MCI_ARG
, card_info
[drive
].csd
))
348 sd_parse_csd(&card_info
[drive
]);
350 #if defined(HAVE_MULTIDRIVE)
351 hs_card
= (card_info
[drive
].speed
== 50000000);
354 /* Boost MCICLK to operating speed */
355 if(drive
== INTERNAL_AS3525
)
356 MCI_CLOCK(drive
) = MCI_HALFSPEED
; /* MCICLK = IDE_CLK/2 = 25 MHz */
357 #if defined(HAVE_MULTIDRIVE)
359 /* MCICLK = PCLK/2 = 31MHz(HS) or PCLK/4 = 15.5 Mhz (STD)*/
360 MCI_CLOCK(drive
) = (hs_card
? MCI_HALFSPEED
: MCI_QUARTERSPEED
);
363 /* CMD7 w/rca: Select card to put it in TRAN state */
364 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_ARG
, NULL
))
369 * enable bank switching
370 * without issuing this command, we only have access to 1/4 of the blocks
371 * of the first bank (0x1E9E00 blocks, which is the size reported in the
374 if(drive
== INTERNAL_AS3525
)
376 const int ret
= sd_select_bank(-1);
380 /* CMD7 w/rca = 0: Select card to put it in STBY state */
381 if(!send_cmd(drive
, SD_SELECT_CARD
, 0, MCI_ARG
, NULL
))
385 /* CMD9 send CSD again, so we got the correct number of blocks */
386 if(!send_cmd(drive
, SD_SEND_CSD
, card_info
[drive
].rca
,
387 MCI_RESP
|MCI_LONG_RESP
|MCI_ARG
, card_info
[drive
].csd
))
390 sd_parse_csd(&card_info
[drive
]);
391 /* The OF is stored in the first blocks */
392 card_info
[INTERNAL_AS3525
].numblocks
-= AMS_OF_SIZE
;
394 /* CMD7 w/rca: Select card to put it in TRAN state */
395 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_ARG
, NULL
))
400 card_info
[drive
].initialized
= 1;
405 static void sd_thread(void) __attribute__((noreturn
));
406 static void sd_thread(void)
408 struct queue_event ev
;
409 bool idle_notified
= false;
413 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
418 case SYS_HOTSWAP_INSERTED
:
419 case SYS_HOTSWAP_EXTRACTED
:
421 int microsd_init
= 1;
422 fat_lock(); /* lock-out FAT activity first -
423 prevent deadlocking via disk_mount that
424 would cause a reverse-order attempt with
426 mutex_lock(&sd_mtx
); /* lock-out card activity - direct calls
427 into driver that bypass the fat cache */
429 /* We now have exclusive control of fat cache and ata */
431 disk_unmount(SD_SLOT_AS3525
); /* release "by force", ensure file
432 descriptors aren't leaked and any busy
433 ones are invalid if mounting */
435 /* Force card init for new card, re-init for re-inserted one or
436 * clear if the last attempt to init failed with an error. */
437 card_info
[SD_SLOT_AS3525
].initialized
= 0;
439 if (ev
.id
== SYS_HOTSWAP_INSERTED
)
442 init_pl180_controller(SD_SLOT_AS3525
);
443 microsd_init
= sd_init_card(SD_SLOT_AS3525
);
444 if (microsd_init
< 0) /* initialisation failed */
445 panicf("microSD init failed : %d", microsd_init
);
447 microsd_init
= disk_mount(SD_SLOT_AS3525
); /* 0 if fail */
451 * Mount succeeded, or this was an EXTRACTED event,
452 * in both cases notify the system about the changed filesystems
455 queue_broadcast(SYS_FS_CHANGED
, 0);
457 /* Access is now safe */
458 mutex_unlock(&sd_mtx
);
465 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
467 idle_notified
= false;
471 /* never let a timer wrap confuse us */
472 next_yield
= current_tick
;
476 call_storage_idle_notifys(false);
477 idle_notified
= true;
482 case SYS_USB_CONNECTED
:
483 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
484 /* Wait until the USB cable is extracted again */
485 usb_wait_for_disconnect(&sd_queue
);
488 case SYS_USB_DISCONNECTED
:
489 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
495 static void init_pl180_controller(const int drive
)
497 MCI_COMMAND(drive
) = MCI_DATA_CTRL(drive
) = 0;
498 MCI_CLEAR(drive
) = 0x7ff;
500 MCI_MASK0(drive
) = MCI_DATA_ERROR
| MCI_DATA_END
;
501 MCI_MASK1(drive
) = 0;
502 #ifdef HAVE_MULTIDRIVE
504 (drive
== INTERNAL_AS3525
) ? INTERRUPT_NAND
: INTERRUPT_MCI0
;
505 /* clear previous irq */
506 GPIOA_IC
= EXT_SD_BITS
;
507 /* enable edge detecting */
508 GPIOA_IS
&= ~EXT_SD_BITS
;
509 /* detect both raising and falling edges */
510 GPIOA_IBE
|= EXT_SD_BITS
;
513 VIC_INT_ENABLE
= INTERRUPT_NAND
;
516 MCI_POWER(drive
) = MCI_POWER_UP
| (MCI_VDD_3_0
); /* OF Setting */
519 MCI_POWER(drive
) |= MCI_POWER_ON
;
522 MCI_SELECT(drive
) = 0;
524 /* Pl180 clocks get turned on at start of card init */
530 CGU_IDE
= (1<<6) /* enable non AHB interface*/
531 | (AS3525_IDE_DIV
<< 2)
532 | AS3525_CLK_PLLA
; /* clock source = PLLA */
534 CGU_PERI
|= CGU_NAF_CLOCK_ENABLE
;
535 #ifdef HAVE_MULTIDRIVE
536 CGU_PERI
|= CGU_MCI_CLOCK_ENABLE
;
537 CCU_IO
&= ~(1<<3); /* bits 3:2 = 01, xpd is SD interface */
541 wakeup_init(&transfer_completion_signal
);
543 init_pl180_controller(INTERNAL_AS3525
);
544 ret
= sd_init_card(INTERNAL_AS3525
);
547 #ifdef HAVE_MULTIDRIVE
548 init_pl180_controller(SD_SLOT_AS3525
);
554 queue_init(&sd_queue
, true);
555 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
556 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
) IF_COP(, CPU
));
566 bool sd_removable(IF_MD_NONVOID(int drive
))
571 bool sd_present(IF_MD_NONVOID(int drive
))
573 return (drive
== 0) ? true : card_detect_target();
575 #endif /* HAVE_HOTSWAP */
577 static int sd_wait_for_tran_state(const int drive
)
579 unsigned long response
= 0;
580 unsigned int timeout
= current_tick
+ 5 * HZ
;
584 if(!send_cmd(drive
, SD_SEND_STATUS
, card_info
[drive
].rca
,
585 MCI_RESP
|MCI_ARG
, &response
))
588 if (((response
>> 9) & 0xf) == SD_TRAN
)
591 if(TIME_AFTER(current_tick
, timeout
))
594 if (TIME_AFTER(current_tick
, next_yield
))
597 next_yield
= current_tick
+ MIN_YIELD_PERIOD
;
602 static int sd_select_bank(signed char bank
)
607 memset(uncached_buffer
, 0, 512);
609 { /* enable bank switching */
610 uncached_buffer
[0] = 16;
611 uncached_buffer
[1] = 1;
612 uncached_buffer
[2] = 10;
615 uncached_buffer
[0] = bank
;
618 if(loops
++ > PL180_MAX_TRANSFER_ERRORS
)
619 panicf("SD bank %d error : 0x%x", bank
,
620 transfer_error
[INTERNAL_AS3525
]);
622 ret
= sd_wait_for_tran_state(INTERNAL_AS3525
);
626 if(!send_cmd(INTERNAL_AS3525
, SD_SWITCH_FUNC
, 0x80ffffef, MCI_ARG
, NULL
))
631 if(!send_cmd(INTERNAL_AS3525
, 35, 0, MCI_NO_FLAGS
, NULL
))
637 /* we don't use the uncached buffer here, because we need the
638 * physical memory address for DMA transfers */
639 dma_enable_channel(0, aligned_buffer
, MCI_FIFO(INTERNAL_AS3525
),
640 DMA_PERI_SD
, DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
,
643 MCI_DATA_TIMER(INTERNAL_AS3525
) = SD_MAX_WRITE_TIMEOUT
;
644 MCI_DATA_LENGTH(INTERNAL_AS3525
) = 512;
645 MCI_DATA_CTRL(INTERNAL_AS3525
) = (1<<0) /* enable */ |
646 (0<<1) /* transfer direction */ |
648 (9<<4) /* 2^9 = 512 */ ;
650 /* Wakeup signal from NAND/MCIO isr on MCI_DATA_ERROR | MCI_DATA_END */
651 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
653 /* Wait for FIFO to empty, card may still be in PRG state */
654 while(MCI_STATUS(INTERNAL_AS3525
) & MCI_TX_ACTIVE
);
658 } while(transfer_error
[INTERNAL_AS3525
]);
660 card_info
[INTERNAL_AS3525
].current_bank
= (bank
== -1) ? 0 : bank
;
665 static int sd_transfer_sectors(IF_MD2(int drive
,) unsigned long start
,
666 int count
, void* buf
, const bool write
)
668 #ifndef HAVE_MULTIDRIVE
680 if (card_info
[drive
].initialized
<= 0)
682 ret
= sd_init_card(drive
);
683 if (!(card_info
[drive
].initialized
))
684 goto sd_transfer_error
;
687 if(count
< 0) /* XXX: why is it signed ? */
690 goto sd_transfer_error
;
692 if((start
+count
) > card_info
[drive
].numblocks
)
695 goto sd_transfer_error
;
698 /* skip SanDisk OF */
699 if (drive
== INTERNAL_AS3525
)
700 start
+= AMS_OF_SIZE
;
702 last_disk_activity
= current_tick
;
708 /* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH
709 * register, so we have to transfer maximum 127 sectors at a time. */
710 unsigned int transfer
= (count
>= 128) ? 127 : count
; /* sectors */
713 write
? SD_WRITE_MULTIPLE_BLOCK
: SD_READ_MULTIPLE_BLOCK
;
714 unsigned long bank_start
= start
;
716 /* Only switch banks for internal storage */
717 if(drive
== INTERNAL_AS3525
)
719 unsigned int bank
= 0;
720 while(bank_start
>= BLOCKS_PER_BANK
)
722 bank_start
-= BLOCKS_PER_BANK
;
726 /* Switch bank if needed */
727 if(card_info
[INTERNAL_AS3525
].current_bank
!= bank
)
729 ret
= sd_select_bank(bank
);
733 goto sd_transfer_error
;
737 /* Do not cross a bank boundary in a single transfer loop */
738 if((transfer
+ bank_start
) > BLOCKS_PER_BANK
)
739 transfer
= BLOCKS_PER_BANK
- bank_start
;
742 /* Set bank_start to the correct unit (blocks or bytes) */
743 if(!(card_info
[drive
].ocr
& (1<<30))) /* not SDHC */
744 bank_start
*= SD_BLOCK_SIZE
;
746 dma_buf
= aligned_buffer
;
747 if(transfer
> UNALIGNED_NUM_SECTORS
)
748 transfer
= UNALIGNED_NUM_SECTORS
;
751 memcpy(uncached_buffer
, buf
, transfer
* SD_BLOCK_SIZE
);
753 ret
= sd_wait_for_tran_state(drive
);
757 goto sd_transfer_error
;
760 if(!send_cmd(drive
, cmd
, bank_start
, MCI_ARG
, NULL
))
763 goto sd_transfer_error
;
768 dma_enable_channel(0, dma_buf
, MCI_FIFO(drive
),
769 (drive
== INTERNAL_AS3525
) ? DMA_PERI_SD
: DMA_PERI_SD_SLOT
,
770 DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
, NULL
);
772 /*Small delay for writes prevents data crc failures at lower freqs*/
773 #ifdef HAVE_MULTIDRIVE
774 if((drive
== SD_SLOT_AS3525
) && !hs_card
)
776 int write_delay
= 125;
777 while(write_delay
--);
782 dma_enable_channel(0, MCI_FIFO(drive
), dma_buf
,
783 (drive
== INTERNAL_AS3525
) ? DMA_PERI_SD
: DMA_PERI_SD_SLOT
,
784 DMAC_FLOWCTRL_PERI_PERI_TO_MEM
, false, true, 0, DMA_S8
, NULL
);
786 MCI_DATA_TIMER(drive
) = write
?
787 SD_MAX_WRITE_TIMEOUT
: SD_MAX_READ_TIMEOUT
;
788 MCI_DATA_LENGTH(drive
) = transfer
* SD_BLOCK_SIZE
;
789 MCI_DATA_CTRL(drive
) = (1<<0) /* enable */ |
790 (!write
<<1) /* transfer direction */ |
792 (9<<4) /* 2^9 = 512 */ ;
794 /* Wakeup signal from NAND/MCIO isr on MCI_DATA_ERROR | MCI_DATA_END */
795 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
797 /* Wait for FIFO to empty, card may still be in PRG state for writes */
798 while(MCI_STATUS(drive
) & MCI_TX_ACTIVE
);
800 last_disk_activity
= current_tick
;
802 if(!send_cmd(drive
, SD_STOP_TRANSMISSION
, 0, MCI_NO_FLAGS
, NULL
))
805 goto sd_transfer_error
;
808 if(!transfer_error
[drive
])
811 memcpy(buf
, uncached_buffer
, transfer
* SD_BLOCK_SIZE
);
812 buf
+= transfer
* SD_BLOCK_SIZE
;
815 loops
= 0; /* reset errors counter */
817 else if(loops
++ > PL180_MAX_TRANSFER_ERRORS
)
818 panicf("SD Xfer %s err:0x%x Disk%d", (write
? "write": "read"),
819 transfer_error
[drive
], drive
);
832 card_info
[drive
].initialized
= 0;
834 mutex_unlock(&sd_mtx
);
838 int sd_read_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
841 return sd_transfer_sectors(IF_MD2(drive
,) start
, count
, buf
, false);
844 int sd_write_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
848 #ifdef BOOTLOADER /* we don't need write support in bootloader */
849 #ifdef HAVE_MULTIDRIVE
857 return sd_transfer_sectors(IF_MD2(drive
,) start
, count
, (void*)buf
, true);
862 long sd_last_disk_activity(void)
864 return last_disk_activity
;
867 void sd_enable(bool on
)
869 #if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE)
870 extern int buttonlight_is_on
;
873 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
874 static bool cpu_boosted
= false;
877 if (sd_enabled
== on
)
878 return; /* nothing to do */
884 #if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE)
885 /* buttonlight AMSes need a bit of special handling for the buttonlight
886 * here due to the dual mapping of GPIOD and XPD */
887 CCU_IO
|= (1<<2); /* XPD is SD-MCI interface (b3:2 = 01) */
888 if (buttonlight_is_on
)
889 GPIOD_DIR
&= ~(1<<7);
894 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
895 if(card_detect_target()) /* If SD card present Boost cpu for voltage */
900 #endif /* defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE) */
904 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
910 #endif /* defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE) */
912 #if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE)
913 CCU_IO
&= ~(1<<2); /* XPD is general purpose IO (b3:2 = 00) */
914 if (buttonlight_is_on
)
920 tCardInfo
*card_get_info_target(int card_no
)
922 return &card_info
[card_no
];
926 void card_enable_monitoring_target(bool on
)
928 if (on
) /* enable interrupt */
929 GPIOA_IE
|= EXT_SD_BITS
;
930 else /* disable interrupt */
931 GPIOA_IE
&= ~EXT_SD_BITS
;
933 #endif /* HAVE_HOTSWAP */
935 #endif /* !BOOTLOADER */
937 #ifdef CONFIG_STORAGE_MULTI
938 int sd_num_drives(int first_drive
)
940 /* We don't care which logical drive number(s) we have been assigned */
945 #endif /* CONFIG_STORAGE_MULTI */