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 static 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 static int sd_init_card(const int drive
)
253 unsigned long response
;
256 unsigned long temp_reg
[4];
260 /* 100 - 400kHz clock required for Identification Mode */
261 MCI_CLOCK(drive
) = (MCI_CLOCK(drive
) & 0xf00) | AS3525_SD_IDENT_DIV
;
264 /* Start of Card Identification Mode ************************************/
268 if(!send_cmd(drive
, SD_GO_IDLE_STATE
, 0, MCI_NO_FLAGS
, NULL
))
273 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
274 Non v2 cards will not respond to this command*/
275 if(send_cmd(drive
, SD_SEND_IF_COND
, 0x1AA, MCI_RESP
|MCI_ARG
, &response
))
276 if((response
& 0xFFF) == 0x1AA)
279 /* timeout for initialization is 1sec, from SD Specification 2.00 */
280 init_timeout
= current_tick
+ HZ
;
283 /* this timeout is the only valid error for this loop*/
284 if(TIME_AFTER(current_tick
, init_timeout
))
288 send_cmd(drive
, SD_APP_CMD
, 0, MCI_RESP
|MCI_ARG
, &response
);
290 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
291 send_cmd(drive
, SD_APP_OP_COND
, (0x00FF8000 | (sd_v2
? 1<<30 : 0)),
292 MCI_RESP
|MCI_ARG
, &card_info
[drive
].ocr
);
294 } while(!(card_info
[drive
].ocr
& (1<<31)));
297 if(!send_cmd(drive
, SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
|MCI_ARG
,
302 card_info
[drive
].cid
[3-i
] = temp_reg
[i
];
305 if(!send_cmd(drive
, SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
|MCI_ARG
,
306 &card_info
[drive
].rca
))
310 /* End of Card Identification Mode ************************************/
313 /* full speed for controller clock MCICLK = MCLK = PCLK = 62 MHz */
314 MCI_CLOCK(drive
) |= MCI_CLOCK_BYPASS
; /* FIXME: 50 MHz is spec limit */
317 #ifdef HAVE_MULTIDRIVE
318 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
321 /* CMD7 w/rca: Select card to put it in TRAN state */
322 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_ARG
, NULL
))
325 if(sd_wait_for_state(drive
, SD_TRAN
))
328 if(!send_cmd(drive
, SD_SWITCH_FUNC
, 0x80fffff1, MCI_ARG
, NULL
))
332 /* go back to STBY state so we can read csd */
333 /* CMD7 w/rca=0: Deselect card to put it in STBY state */
334 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
, temp_reg
))
345 card_info
[drive
].csd
[3-i
] = temp_reg
[i
];
347 sd_parse_csd(&card_info
[drive
]);
349 /* CMD7 w/rca: Select card to put it in TRAN state */
350 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_ARG
, NULL
))
354 * enable bank switching
355 * without issuing this command, we only have access to 1/4 of the blocks
356 * of the first bank (0x1E9E00 blocks, which is the size reported in the
359 if(drive
== INTERNAL_AS3525
)
361 const int ret
= sd_select_bank(-1);
366 card_info
[drive
].initialized
= 1;
371 static void sd_thread(void) __attribute__((noreturn
));
372 static void sd_thread(void)
374 struct queue_event ev
;
375 bool idle_notified
= false;
379 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
384 case SYS_HOTSWAP_INSERTED
:
385 case SYS_HOTSWAP_EXTRACTED
:
387 int microsd_init
= 1;
388 fat_lock(); /* lock-out FAT activity first -
389 prevent deadlocking via disk_mount that
390 would cause a reverse-order attempt with
392 mutex_lock(&sd_mtx
); /* lock-out card activity - direct calls
393 into driver that bypass the fat cache */
395 /* We now have exclusive control of fat cache and ata */
397 disk_unmount(SD_SLOT_AS3525
); /* release "by force", ensure file
398 descriptors aren't leaked and any busy
399 ones are invalid if mounting */
401 /* Force card init for new card, re-init for re-inserted one or
402 * clear if the last attempt to init failed with an error. */
403 card_info
[SD_SLOT_AS3525
].initialized
= 0;
405 if (ev
.id
== SYS_HOTSWAP_INSERTED
)
408 init_pl180_controller(SD_SLOT_AS3525
);
409 microsd_init
= sd_init_card(SD_SLOT_AS3525
);
410 if (microsd_init
< 0) /* initialisation failed */
411 panicf("microSD init failed : %d", microsd_init
);
413 microsd_init
= disk_mount(SD_SLOT_AS3525
); /* 0 if fail */
417 * Mount succeeded, or this was an EXTRACTED event,
418 * in both cases notify the system about the changed filesystems
421 queue_broadcast(SYS_FS_CHANGED
, 0);
423 /* Access is now safe */
424 mutex_unlock(&sd_mtx
);
431 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
433 idle_notified
= false;
437 /* never let a timer wrap confuse us */
438 next_yield
= current_tick
;
442 call_storage_idle_notifys(false);
443 idle_notified
= true;
448 case SYS_USB_CONNECTED
:
449 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
450 /* Wait until the USB cable is extracted again */
451 usb_wait_for_disconnect(&sd_queue
);
454 case SYS_USB_DISCONNECTED
:
455 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
461 static void init_pl180_controller(const int drive
)
463 MCI_COMMAND(drive
) = MCI_DATA_CTRL(drive
) = 0;
464 MCI_CLEAR(drive
) = 0x7ff;
466 MCI_MASK0(drive
) = MCI_ERROR
| MCI_DATA_END
;
467 MCI_MASK1(drive
) = 0;
468 #ifdef HAVE_MULTIDRIVE
470 (drive
== INTERNAL_AS3525
) ? INTERRUPT_NAND
: INTERRUPT_MCI0
;
472 /* setup isr for microsd monitoring */
473 VIC_INT_ENABLE
= (INTERRUPT_GPIOA
);
474 /* clear previous irq */
476 /* enable edge detecting */
478 /* detect both raising and falling edges */
482 VIC_INT_ENABLE
= INTERRUPT_NAND
;
485 MCI_POWER(drive
) = MCI_POWER_UP
| (MCI_VDD_3_0
); /* OF Setting */
488 MCI_POWER(drive
) |= MCI_POWER_ON
;
491 MCI_SELECT(drive
) = 0;
493 MCI_CLOCK(drive
) = MCI_CLOCK_ENABLE
;
500 CGU_IDE
= (1<<7) /* AHB interface enable */ |
501 (1<<6) /* interface enable */ |
502 (AS3525_IDE_DIV
<< 2) |
503 AS3525_CLK_PLLA
; /* clock source = PLLA */
506 CGU_PERI
|= CGU_NAF_CLOCK_ENABLE
;
507 #ifdef HAVE_MULTIDRIVE
508 CGU_PERI
|= CGU_MCI_CLOCK_ENABLE
;
509 CCU_IO
&= ~(1<<3); /* bits 3:2 = 01, xpd is SD interface */
513 wakeup_init(&transfer_completion_signal
);
515 init_pl180_controller(INTERNAL_AS3525
);
516 ret
= sd_init_card(INTERNAL_AS3525
);
519 #ifdef HAVE_MULTIDRIVE
520 init_pl180_controller(SD_SLOT_AS3525
);
526 queue_init(&sd_queue
, true);
527 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
528 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
) IF_COP(, CPU
));
538 bool sd_removable(IF_MD_NONVOID(int drive
))
543 bool sd_present(IF_MD_NONVOID(int drive
))
545 return (drive
== 0) ? true : card_detect_target();
547 #endif /* HAVE_HOTSWAP */
549 static int sd_wait_for_state(const int drive
, unsigned int state
)
551 unsigned long response
= 0;
552 unsigned int timeout
= current_tick
+ 100; /* 100 ticks timeout */
556 if(!send_cmd(drive
, SD_SEND_STATUS
, card_info
[drive
].rca
,
557 MCI_RESP
|MCI_ARG
, &response
))
560 if (((response
>> 9) & 0xf) == state
)
563 if(TIME_AFTER(current_tick
, timeout
))
566 if (TIME_AFTER(current_tick
, next_yield
))
569 next_yield
= current_tick
+ MIN_YIELD_PERIOD
;
574 static int sd_select_bank(signed char bank
)
580 if(loops
++ > PL180_MAX_TRANSFER_ERRORS
)
581 panicf("SD bank %d error : 0x%x", bank
,
582 transfer_error
[INTERNAL_AS3525
]);
584 ret
= sd_wait_for_state(INTERNAL_AS3525
, SD_TRAN
);
588 if(!send_cmd(INTERNAL_AS3525
, SD_SWITCH_FUNC
, 0x80ffffef, MCI_ARG
, NULL
))
593 if(!send_cmd(INTERNAL_AS3525
, 35, 0, MCI_NO_FLAGS
, NULL
))
598 memset(uncached_buffer
, 0, 512);
600 { /* enable bank switching */
601 uncached_buffer
[0] = 16;
602 uncached_buffer
[1] = 1;
603 uncached_buffer
[2] = 10;
606 uncached_buffer
[0] = bank
;
609 /* we don't use the uncached buffer here, because we need the
610 * physical memory address for DMA transfers */
611 dma_enable_channel(0, aligned_buffer
, MCI_FIFO(INTERNAL_AS3525
),
612 DMA_PERI_SD
, DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
,
615 MCI_DATA_TIMER(INTERNAL_AS3525
) = SD_MAX_WRITE_TIMEOUT
;
616 MCI_DATA_LENGTH(INTERNAL_AS3525
) = 512;
617 MCI_DATA_CTRL(INTERNAL_AS3525
) = (1<<0) /* enable */ |
618 (0<<1) /* transfer direction */ |
620 (9<<4) /* 2^9 = 512 */ ;
622 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
624 /* Wait for FIFO to empty, card may still be in PRG state */
625 while(MCI_STATUS(INTERNAL_AS3525
) & MCI_TX_ACTIVE
);
629 } while(transfer_error
[INTERNAL_AS3525
]);
631 card_info
[INTERNAL_AS3525
].current_bank
= (bank
== -1) ? 0 : bank
;
636 static int sd_transfer_sectors(IF_MD2(int drive
,) unsigned long start
,
637 int count
, void* buf
, const bool write
)
639 #ifndef HAVE_MULTIDRIVE
645 /* skip SanDisk OF */
646 if (drive
== INTERNAL_AS3525
)
647 start
+= AMS_OF_SIZE
;
655 if (card_info
[drive
].initialized
<= 0)
657 ret
= sd_init_card(drive
);
658 if (!(card_info
[drive
].initialized
))
659 goto sd_transfer_error
;
662 last_disk_activity
= current_tick
;
668 /* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH
669 * register, so we have to transfer maximum 127 sectors at a time. */
670 unsigned int transfer
= (count
>= 128) ? 127 : count
; /* sectors */
673 write
? SD_WRITE_MULTIPLE_BLOCK
: SD_READ_MULTIPLE_BLOCK
;
674 unsigned long bank_start
= start
;
676 /* Only switch banks for internal storage */
677 if(drive
== INTERNAL_AS3525
)
679 unsigned int bank
= start
/ BLOCKS_PER_BANK
; /* Current bank */
681 /* Switch bank if needed */
682 if(card_info
[INTERNAL_AS3525
].current_bank
!= bank
)
684 ret
= sd_select_bank(bank
);
688 goto sd_transfer_error
;
692 /* Adjust start block in current bank */
693 bank_start
-= bank
* BLOCKS_PER_BANK
;
695 /* Do not cross a bank boundary in a single transfer loop */
696 if((transfer
+ bank_start
) > BLOCKS_PER_BANK
)
697 transfer
= BLOCKS_PER_BANK
- bank_start
;
700 dma_buf
= aligned_buffer
;
701 if(transfer
> UNALIGNED_NUM_SECTORS
)
702 transfer
= UNALIGNED_NUM_SECTORS
;
704 memcpy(uncached_buffer
, buf
, transfer
* SD_BLOCK_SIZE
);
706 /* Set bank_start to the correct unit (blocks or bytes) */
707 if(!(card_info
[drive
].ocr
& (1<<30))) /* not SDHC */
708 bank_start
*= SD_BLOCK_SIZE
;
710 ret
= sd_wait_for_state(drive
, SD_TRAN
);
714 goto sd_transfer_error
;
717 if(!send_cmd(drive
, cmd
, bank_start
, MCI_ARG
, NULL
))
720 goto sd_transfer_error
;
724 dma_enable_channel(0, dma_buf
, MCI_FIFO(drive
),
725 (drive
== INTERNAL_AS3525
) ? DMA_PERI_SD
: DMA_PERI_SD_SLOT
,
726 DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
, NULL
);
728 dma_enable_channel(0, MCI_FIFO(drive
), dma_buf
,
729 (drive
== INTERNAL_AS3525
) ? DMA_PERI_SD
: DMA_PERI_SD_SLOT
,
730 DMAC_FLOWCTRL_PERI_PERI_TO_MEM
, false, true, 0, DMA_S8
, NULL
);
732 /* FIXME : we should check if the timeouts calculated from the card's
733 * CSD are lower, and use them if it is the case
734 * Note : the OF doesn't seem to use them anyway */
735 MCI_DATA_TIMER(drive
) = write
?
736 SD_MAX_WRITE_TIMEOUT
: SD_MAX_READ_TIMEOUT
;
737 MCI_DATA_LENGTH(drive
) = transfer
* SD_BLOCK_SIZE
;
738 MCI_DATA_CTRL(drive
) = (1<<0) /* enable */ |
739 (!write
<<1) /* transfer direction */ |
741 (9<<4) /* 2^9 = 512 */ ;
744 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
746 /* Wait for FIFO to empty, card may still be in PRG state for writes */
747 while(MCI_STATUS(drive
) & MCI_TX_ACTIVE
);
749 if(!transfer_error
[drive
])
752 memcpy(buf
, uncached_buffer
, transfer
* SD_BLOCK_SIZE
);
753 buf
+= transfer
* SD_BLOCK_SIZE
;
756 loops
= 0; /* reset errors counter */
758 else if(loops
++ > PL180_MAX_TRANSFER_ERRORS
)
759 panicf("SD transfer error : 0x%x", transfer_error
[drive
]);
761 last_disk_activity
= current_tick
;
763 if(!send_cmd(drive
, SD_STOP_TRANSMISSION
, 0, MCI_NO_FLAGS
, NULL
))
766 goto sd_transfer_error
;
770 ret
= 0; /* success */
782 card_info
[drive
].initialized
= 0;
784 mutex_unlock(&sd_mtx
);
788 int sd_read_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
791 return sd_transfer_sectors(IF_MD2(drive
,) start
, count
, buf
, false);
794 int sd_write_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
798 #ifdef BOOTLOADER /* we don't need write support in bootloader */
799 #ifdef HAVE_MULTIDRIVE
807 return sd_transfer_sectors(IF_MD2(drive
,) start
, count
, (void*)buf
, true);
812 long sd_last_disk_activity(void)
814 return last_disk_activity
;
817 void sd_enable(bool on
)
819 /* buttonlight AMSes need a bit of special handling for the buttonlight here
820 * due to the dual mapping of GPIOD and XPD */
821 #if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE)
822 extern int buttonlight_is_on
;
826 static bool cpu_boosted
= false;
829 if (sd_enabled
== on
)
830 return; /* nothing to do */
833 CGU_PERI
|= CGU_NAF_CLOCK_ENABLE
;
834 #ifdef HAVE_MULTIDRIVE
835 CGU_PERI
|= CGU_MCI_CLOCK_ENABLE
;
836 #ifdef HAVE_BUTTON_LIGHT
838 if (buttonlight_is_on
)
839 GPIOD_DIR
&= ~(1<<7);
842 #endif /* HAVE_BUTTON_LIGHT */
843 #endif /* HAVE_MULTIDRIVE */
844 CGU_IDE
|= (1<<7) /* AHB interface enable */ |
845 (1<<6) /* interface enable */;
849 if(card_detect_target()) /* If SD card present Boost cpu for voltage */
858 CGU_PERI
&= ~CGU_NAF_CLOCK_ENABLE
;
859 #ifdef HAVE_MULTIDRIVE
860 #ifdef HAVE_BUTTON_LIGHT
862 if (buttonlight_is_on
)
864 #endif /* HAVE_BUTTON_LIGHT */
865 CGU_PERI
&= ~CGU_MCI_CLOCK_ENABLE
;
866 #endif /* HAVE_MULTIDRIVE */
867 CGU_IDE
&= ~((1<<7)|(1<<6));
880 tCardInfo
*card_get_info_target(int card_no
)
882 return &card_info
[card_no
];
886 void card_enable_monitoring_target(bool on
)
888 if (on
) /* enable interrupt */
890 else /* disable interrupt */
893 #endif /* HAVE_HOTSWAP */
895 #endif /* !BOOTLOADER */
897 #ifdef CONFIG_STORAGE_MULTI
898 int sd_num_drives(int first_drive
)
900 /* We don't care which logical drive number(s) we have been assigned */
905 #endif /* CONFIG_STORAGE_MULTI */