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"
48 /*#define LOGF_ENABLE*/
55 #define VERIFY_WRITE 1
58 #define MCI_NO_RESP (0<<0)
59 #define MCI_RESP (1<<0)
60 #define MCI_LONG_RESP (1<<1)
61 #define MCI_ACMD (1<<2)
62 #define MCI_NOCRC (1<<3)
64 /* ARM PL180 registers */
65 #define MCI_POWER(i) (*(volatile unsigned char *) (pl180_base[i]+0x00))
66 #define MCI_CLOCK(i) (*(volatile unsigned long *) (pl180_base[i]+0x04))
67 #define MCI_ARGUMENT(i) (*(volatile unsigned long *) (pl180_base[i]+0x08))
68 #define MCI_COMMAND(i) (*(volatile unsigned long *) (pl180_base[i]+0x0C))
69 #define MCI_RESPCMD(i) (*(volatile unsigned long *) (pl180_base[i]+0x10))
70 #define MCI_RESP0(i) (*(volatile unsigned long *) (pl180_base[i]+0x14))
71 #define MCI_RESP1(i) (*(volatile unsigned long *) (pl180_base[i]+0x18))
72 #define MCI_RESP2(i) (*(volatile unsigned long *) (pl180_base[i]+0x1C))
73 #define MCI_RESP3(i) (*(volatile unsigned long *) (pl180_base[i]+0x20))
74 #define MCI_DATA_TIMER(i) (*(volatile unsigned long *) (pl180_base[i]+0x24))
75 #define MCI_DATA_LENGTH(i) (*(volatile unsigned short*) (pl180_base[i]+0x28))
76 #define MCI_DATA_CTRL(i) (*(volatile unsigned char *) (pl180_base[i]+0x2C))
77 #define MCI_DATA_CNT(i) (*(volatile unsigned short*) (pl180_base[i]+0x30))
78 #define MCI_STATUS(i) (*(volatile unsigned long *) (pl180_base[i]+0x34))
79 #define MCI_CLEAR(i) (*(volatile unsigned long *) (pl180_base[i]+0x38))
80 #define MCI_MASK0(i) (*(volatile unsigned long *) (pl180_base[i]+0x3C))
81 #define MCI_MASK1(i) (*(volatile unsigned long *) (pl180_base[i]+0x40))
82 #define MCI_SELECT(i) (*(volatile unsigned long *) (pl180_base[i]+0x44))
83 #define MCI_FIFO_CNT(i) (*(volatile unsigned long *) (pl180_base[i]+0x48))
85 #define MCI_DATA_ERROR \
92 #define MCI_RESPONSE_ERROR \
96 #define MCI_FIFO(i) ((unsigned long *) (pl180_base[i]+0x80))
98 #define INTERNAL_AS3525 0 /* embedded SD card */
99 #define SD_SLOT_AS3525 1 /* SD slot if present */
101 static const int pl180_base
[NUM_DRIVES
] = {
103 #ifdef HAVE_MULTIDRIVE
108 static int sd_wait_for_tran_state(const int drive
);
109 static int sd_select_bank(signed char bank
);
110 static int sd_init_card(const int drive
);
111 static void init_pl180_controller(const int drive
);
113 #define BLOCKS_PER_BANK 0x7a7800u
115 static tCardInfo card_info
[NUM_DRIVES
];
117 /* maximum timeouts recommanded in the SD Specification v2.00 */
118 #define SD_MAX_READ_TIMEOUT ((AS3525_PCLK_FREQ) / 1000 * 100) /* 100 ms */
119 #define SD_MAX_WRITE_TIMEOUT ((AS3525_PCLK_FREQ) / 1000 * 250) /* 250 ms */
121 /* for compatibility */
122 static long last_disk_activity
= -1;
124 #define MIN_YIELD_PERIOD 5 /* ticks */
125 static long next_yield
= 0;
127 static long sd_stack
[(DEFAULT_STACK_SIZE
*2 + 0x200)/sizeof(long)];
128 static const char sd_thread_name
[] = "ata/sd";
129 static struct mutex sd_mtx
;
130 static struct event_queue sd_queue
;
131 bool sd_enabled
= false;
133 #if defined(HAVE_MULTIDRIVE)
134 static bool hs_card
= false;
135 #define EXT_SD_BITS (1<<2)
138 static struct wakeup transfer_completion_signal
;
139 static volatile unsigned int transfer_error
[NUM_VOLUMES
];
140 #define PL180_MAX_TRANSFER_ERRORS 10
142 #define UNALIGNED_NUM_SECTORS 10
143 static unsigned char aligned_buffer
[UNALIGNED_NUM_SECTORS
* SD_BLOCK_SIZE
] __attribute__((aligned(32))); /* align on cache line size */
144 static unsigned char *uncached_buffer
= AS3525_UNCACHED_ADDR(&aligned_buffer
[0]);
147 static inline void mci_delay(void) { udelay(1000) ; }
150 static inline bool card_detect_target(void)
152 #if defined(HAVE_MULTIDRIVE)
153 return !(GPIOA_PIN(2));
161 static int sd1_oneshot_callback(struct timeout
*tmo
)
165 /* This is called only if the state was stable for 300ms - check state
166 * and post appropriate event. */
167 if (card_detect_target())
169 queue_broadcast(SYS_HOTSWAP_INSERTED
, 0);
172 queue_broadcast(SYS_HOTSWAP_EXTRACTED
, 0);
177 void sd_gpioa_isr(void)
179 static struct timeout sd1_oneshot
;
180 if (GPIOA_MIS
& EXT_SD_BITS
)
181 timeout_register(&sd1_oneshot
, sd1_oneshot_callback
, (3*HZ
/10), 0);
182 /* acknowledge interrupt */
183 GPIOA_IC
= EXT_SD_BITS
;
185 #endif /* HAVE_HOTSWAP */
189 const int status
= MCI_STATUS(INTERNAL_AS3525
);
191 transfer_error
[INTERNAL_AS3525
] = status
& MCI_DATA_ERROR
;
193 wakeup_signal(&transfer_completion_signal
);
194 MCI_CLEAR(INTERNAL_AS3525
) = status
;
197 #ifdef HAVE_MULTIDRIVE
200 const int status
= MCI_STATUS(SD_SLOT_AS3525
);
202 transfer_error
[SD_SLOT_AS3525
] = status
& MCI_DATA_ERROR
;
204 wakeup_signal(&transfer_completion_signal
);
205 MCI_CLEAR(SD_SLOT_AS3525
) = status
;
209 static bool send_cmd(const int drive
, const int cmd
, const int arg
,
210 const int flags
, long *response
)
214 unsigned cmd_retries
= 6;
217 if ((flags
& MCI_ACMD
) && /* send SD_APP_CMD before each try */
218 !send_cmd(drive
, SD_APP_CMD
, card_info
[drive
].rca
, MCI_RESP
, response
))
221 /* Clear old status flags */
222 MCI_CLEAR(drive
) = 0x7ff;
224 /* Load command argument or clear if none */
225 MCI_ARGUMENT(drive
) = arg
;
227 /* Construct MCI_COMMAND & enable CPSM */
230 /* b6 */| ((flags
& (MCI_RESP
|MCI_LONG_RESP
)) ? MCI_COMMAND_RESPONSE
: 0)
231 /* b7 */| ((flags
& MCI_LONG_RESP
) ? MCI_COMMAND_LONG_RESPONSE
: 0)
232 /* b8 | MCI_COMMAND_INTERRUPT */
233 /* b9 | MCI_COMMAND_PENDING */ /*Only used with stream data transfer*/
234 /* b10*/| MCI_COMMAND_ENABLE
; /* Enables CPSM */
236 /* Wait while cmd completes then disable CPSM */
237 while(MCI_STATUS(drive
) & MCI_CMD_ACTIVE
);
238 MCI_COMMAND(drive
) = 0;
240 status
= MCI_STATUS(drive
);
242 /* Handle command responses */
243 if(flags
& MCI_RESP
) /* CMD expects response */
245 response
[0] = MCI_RESP0(drive
); /* Always prepare short response */
247 if(status
& MCI_RESPONSE_ERROR
) {/* timeout or crc failure */
248 if ((status
& MCI_CMD_CRC_FAIL
) &&
251 logf("sd cmd error: drive %d cmd %d arg %08x sd_status %08x resp0 %08lx",
252 drive
, cmd
, arg
, status
, response
[0]);
256 if((flags
& MCI_RESP
) &&
257 !(flags
& MCI_LONG_RESP
) &&
258 (response
[0] & SD_R1_CARD_ERROR
)) {
259 logf("sd card error: drive %d cmd %d arg %08x r1 %08lx",
260 drive
, cmd
, arg
, response
[0]);
263 if(status
& MCI_CMD_RESP_END
) /* Response passed CRC check */
265 if(flags
& MCI_LONG_RESP
)
266 { /* response[0] has already been read */
267 response
[1] = MCI_RESP1(drive
);
268 response
[2] = MCI_RESP2(drive
);
269 response
[3] = MCI_RESP3(drive
);
274 else if(status
& MCI_CMD_SENT
) /* CMD sent, no response required */
281 #define MCI_FULLSPEED (MCI_CLOCK_ENABLE | MCI_CLOCK_BYPASS) /* MCLK */
282 #define MCI_HALFSPEED (MCI_CLOCK_ENABLE) /* MCLK/2 */
283 #define MCI_QUARTERSPEED (MCI_CLOCK_ENABLE | 1) /* MCLK/4 */
284 #define MCI_IDENTSPEED (MCI_CLOCK_ENABLE | AS3525_SD_IDENT_DIV) /* IDENT */
286 static int sd_init_card(const int drive
)
288 unsigned long response
;
292 card_info
[drive
].rca
= 0;
294 /* MCLCK on and set to 400kHz ident frequency */
295 MCI_CLOCK(drive
) = MCI_IDENTSPEED
;
297 /* 100 - 400kHz clock required for Identification Mode */
298 /* Start of Card Identification Mode ************************************/
301 if(!send_cmd(drive
, SD_GO_IDLE_STATE
, 0, MCI_NO_RESP
, NULL
))
305 /* CMD8 Check for v2 sd card. Must be sent before using ACMD41
306 Non v2 cards will not respond to this command*/
307 if(send_cmd(drive
, SD_SEND_IF_COND
, 0x1AA, MCI_RESP
, &response
))
308 if((response
& 0xFFF) == 0x1AA)
311 /* timeout for initialization is 1sec, from SD Specification 2.00 */
312 init_timeout
= current_tick
+ HZ
;
315 /* this timeout is the only valid error for this loop*/
316 if(TIME_AFTER(current_tick
, init_timeout
))
319 /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
320 send_cmd(drive
, SD_APP_OP_COND
, (0x00FF8000 | (sd_v2
? 1<<30 : 0)),
321 MCI_ACMD
|MCI_NOCRC
|MCI_RESP
, &card_info
[drive
].ocr
);
323 } while(!(card_info
[drive
].ocr
& (1<<31)));
326 if(!send_cmd(drive
, SD_ALL_SEND_CID
, 0, MCI_RESP
|MCI_LONG_RESP
,
327 card_info
[drive
].cid
))
331 if(!send_cmd(drive
, SD_SEND_RELATIVE_ADDR
, 0, MCI_RESP
,
332 &card_info
[drive
].rca
))
335 /* End of Card Identification Mode ************************************/
337 #ifdef HAVE_MULTIDRIVE /* The internal SDs are v1 */
339 /* Try to switch V2 cards to HS timings, non HS seem to ignore this */
342 /* CMD7 w/rca: Select card to put it in TRAN state */
343 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_RESP
, &response
))
346 if(sd_wait_for_tran_state(drive
))
349 if(!send_cmd(drive
, SD_SWITCH_FUNC
, 0x80fffff1, MCI_NO_RESP
, NULL
))
353 /* go back to STBY state so we can read csd */
354 /* CMD7 w/rca=0: Deselect card to put it in STBY state */
355 if(!send_cmd(drive
, SD_DESELECT_CARD
, 0, MCI_NO_RESP
, NULL
))
359 #endif /* HAVE_MULTIDRIVE */
362 if(!send_cmd(drive
, SD_SEND_CSD
, card_info
[drive
].rca
,
363 MCI_RESP
|MCI_LONG_RESP
, card_info
[drive
].csd
))
366 sd_parse_csd(&card_info
[drive
]);
368 #if defined(HAVE_MULTIDRIVE)
369 hs_card
= (card_info
[drive
].speed
== 50000000);
372 /* Boost MCICLK to operating speed */
373 if(drive
== INTERNAL_AS3525
)
374 MCI_CLOCK(drive
) = MCI_HALFSPEED
; /* MCICLK = IDE_CLK/2 = 25 MHz */
375 #if defined(HAVE_MULTIDRIVE)
377 /* MCICLK = PCLK/2 = 31MHz(HS) or PCLK/4 = 15.5 Mhz (STD)*/
378 MCI_CLOCK(drive
) = (hs_card
? MCI_HALFSPEED
: MCI_QUARTERSPEED
);
381 /* CMD7 w/rca: Select card to put it in TRAN state */
382 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_RESP
, &response
))
385 /* Switch to to 4 bit widebus mode */
386 if(sd_wait_for_tran_state(drive
) < 0)
389 if(!send_cmd(drive
, SD_SET_CLR_CARD_DETECT
, 0, MCI_ACMD
|MCI_RESP
, &response
))
392 if(!send_cmd(drive
, SD_SET_BUS_WIDTH
, 2, MCI_ACMD
|MCI_RESP
, &response
))
394 /* Now that card is widebus make controller aware */
395 MCI_CLOCK(drive
) |= MCI_CLOCK_WIDEBUS
;
398 * enable bank switching
399 * without issuing this command, we only have access to 1/4 of the blocks
400 * of the first bank (0x1E9E00 blocks, which is the size reported in the
403 if(drive
== INTERNAL_AS3525
)
405 const int ret
= sd_select_bank(-1);
409 /* CMD7 w/rca = 0: Unselect card to put it in STBY state */
410 if(!send_cmd(drive
, SD_SELECT_CARD
, 0, MCI_NO_RESP
, NULL
))
414 /* CMD9 send CSD again, so we got the correct number of blocks */
415 if(!send_cmd(drive
, SD_SEND_CSD
, card_info
[drive
].rca
,
416 MCI_RESP
|MCI_LONG_RESP
, card_info
[drive
].csd
))
419 sd_parse_csd(&card_info
[drive
]);
420 /* The OF is stored in the first blocks */
421 card_info
[INTERNAL_AS3525
].numblocks
-= AMS_OF_SIZE
;
423 /* CMD7 w/rca: Select card to put it in TRAN state */
424 if(!send_cmd(drive
, SD_SELECT_CARD
, card_info
[drive
].rca
, MCI_RESP
, &response
))
428 card_info
[drive
].initialized
= 1;
433 static void sd_thread(void) __attribute__((noreturn
));
434 static void sd_thread(void)
436 struct queue_event ev
;
437 bool idle_notified
= false;
441 queue_wait_w_tmo(&sd_queue
, &ev
, HZ
);
446 case SYS_HOTSWAP_INSERTED
:
447 case SYS_HOTSWAP_EXTRACTED
:
449 int microsd_init
= 1;
450 fat_lock(); /* lock-out FAT activity first -
451 prevent deadlocking via disk_mount that
452 would cause a reverse-order attempt with
454 mutex_lock(&sd_mtx
); /* lock-out card activity - direct calls
455 into driver that bypass the fat cache */
457 /* We now have exclusive control of fat cache and ata */
459 disk_unmount(SD_SLOT_AS3525
); /* release "by force", ensure file
460 descriptors aren't leaked and any busy
461 ones are invalid if mounting */
463 /* Force card init for new card, re-init for re-inserted one or
464 * clear if the last attempt to init failed with an error. */
465 card_info
[SD_SLOT_AS3525
].initialized
= 0;
467 if (ev
.id
== SYS_HOTSWAP_INSERTED
)
470 init_pl180_controller(SD_SLOT_AS3525
);
471 microsd_init
= sd_init_card(SD_SLOT_AS3525
);
472 if (microsd_init
< 0) /* initialisation failed */
473 panicf("microSD init failed : %d", microsd_init
);
475 microsd_init
= disk_mount(SD_SLOT_AS3525
); /* 0 if fail */
479 * Mount succeeded, or this was an EXTRACTED event,
480 * in both cases notify the system about the changed filesystems
483 queue_broadcast(SYS_FS_CHANGED
, 0);
485 /* Access is now safe */
486 mutex_unlock(&sd_mtx
);
493 if (TIME_BEFORE(current_tick
, last_disk_activity
+(3*HZ
)))
495 idle_notified
= false;
499 /* never let a timer wrap confuse us */
500 next_yield
= current_tick
;
504 call_storage_idle_notifys(false);
505 idle_notified
= true;
510 case SYS_USB_CONNECTED
:
511 usb_acknowledge(SYS_USB_CONNECTED_ACK
);
512 /* Wait until the USB cable is extracted again */
513 usb_wait_for_disconnect(&sd_queue
);
516 case SYS_USB_DISCONNECTED
:
517 usb_acknowledge(SYS_USB_DISCONNECTED_ACK
);
523 static void init_pl180_controller(const int drive
)
525 MCI_COMMAND(drive
) = MCI_DATA_CTRL(drive
) = 0;
526 MCI_CLEAR(drive
) = 0x7ff;
528 MCI_MASK0(drive
) = MCI_DATA_ERROR
| MCI_DATA_END
;
529 MCI_MASK1(drive
) = 0;
530 #ifdef HAVE_MULTIDRIVE
532 (drive
== INTERNAL_AS3525
) ? INTERRUPT_NAND
: INTERRUPT_MCI0
;
533 /* clear previous irq */
534 GPIOA_IC
= EXT_SD_BITS
;
535 /* enable edge detecting */
536 GPIOA_IS
&= ~EXT_SD_BITS
;
537 /* detect both raising and falling edges */
538 GPIOA_IBE
|= EXT_SD_BITS
;
539 /* enable the card detect interrupt */
540 GPIOA_IE
|= EXT_SD_BITS
;
543 VIC_INT_ENABLE
= INTERRUPT_NAND
;
546 MCI_POWER(drive
) = MCI_POWER_UP
| (MCI_VDD_3_0
); /* OF Setting */
549 MCI_POWER(drive
) |= MCI_POWER_ON
;
552 MCI_SELECT(drive
) = 0;
554 /* Pl180 clocks get turned on at start of card init */
560 CGU_IDE
= (1<<6) /* enable non AHB interface*/
561 | (AS3525_IDE_DIV
<< 2)
562 | AS3525_CLK_PLLA
; /* clock source = PLLA */
564 bitset32(&CGU_PERI
, CGU_NAF_CLOCK_ENABLE
);
565 #ifdef HAVE_MULTIDRIVE
566 bitset32(&CGU_PERI
, CGU_MCI_CLOCK_ENABLE
);
567 CCU_IO
&= ~(1<<3); /* bits 3:2 = 01, xpd is SD interface */
571 wakeup_init(&transfer_completion_signal
);
573 init_pl180_controller(INTERNAL_AS3525
);
574 ret
= sd_init_card(INTERNAL_AS3525
);
577 #ifdef HAVE_MULTIDRIVE
578 init_pl180_controller(SD_SLOT_AS3525
);
584 queue_init(&sd_queue
, true);
585 create_thread(sd_thread
, sd_stack
, sizeof(sd_stack
), 0,
586 sd_thread_name
IF_PRIO(, PRIORITY_USER_INTERFACE
) IF_COP(, CPU
));
595 bool sd_removable(IF_MD_NONVOID(int drive
))
600 bool sd_present(IF_MD_NONVOID(int drive
))
602 return (drive
== 0) ? true : card_detect_target();
604 #endif /* HAVE_HOTSWAP */
606 static int sd_wait_for_tran_state(const int drive
)
608 unsigned long response
= 0;
609 unsigned int timeout
= current_tick
+ 5 * HZ
;
613 if(!send_cmd(drive
, SD_SEND_STATUS
, card_info
[drive
].rca
, MCI_RESP
,
617 if (((response
>> 9) & 0xf) == SD_TRAN
)
620 if(TIME_AFTER(current_tick
, timeout
))
623 if (TIME_AFTER(current_tick
, next_yield
))
626 next_yield
= current_tick
+ MIN_YIELD_PERIOD
;
631 static int sd_select_bank(signed char bank
)
636 memset(uncached_buffer
, 0, 512);
638 { /* enable bank switching */
639 uncached_buffer
[0] = 16;
640 uncached_buffer
[1] = 1;
641 uncached_buffer
[2] = 10;
644 uncached_buffer
[0] = bank
;
647 if(loops
++ > PL180_MAX_TRANSFER_ERRORS
)
648 panicf("SD bank %d error : 0x%x", bank
,
649 transfer_error
[INTERNAL_AS3525
]);
651 ret
= sd_wait_for_tran_state(INTERNAL_AS3525
);
655 if(!send_cmd(INTERNAL_AS3525
, SD_SWITCH_FUNC
, 0x80ffffef, MCI_NO_RESP
,
661 if(!send_cmd(INTERNAL_AS3525
, 35, 0, MCI_NO_RESP
, NULL
))
667 /* we don't use the uncached buffer here, because we need the
668 * physical memory address for DMA transfers */
669 dma_enable_channel(0, aligned_buffer
, MCI_FIFO(INTERNAL_AS3525
),
670 DMA_PERI_SD
, DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
,
673 MCI_DATA_TIMER(INTERNAL_AS3525
) = SD_MAX_WRITE_TIMEOUT
;
674 MCI_DATA_LENGTH(INTERNAL_AS3525
) = 512;
675 MCI_DATA_CTRL(INTERNAL_AS3525
) = (1<<0) /* enable */ |
676 (0<<1) /* transfer direction */ |
678 (9<<4) /* 2^9 = 512 */ ;
680 /* Wakeup signal from NAND/MCIO isr on MCI_DATA_ERROR | MCI_DATA_END */
681 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
683 /* Wait for FIFO to empty, card may still be in PRG state */
684 while(MCI_STATUS(INTERNAL_AS3525
) & MCI_TX_ACTIVE
);
688 } while(transfer_error
[INTERNAL_AS3525
]);
690 card_info
[INTERNAL_AS3525
].current_bank
= (bank
== -1) ? 0 : bank
;
695 static int sd_transfer_sectors(IF_MD2(int drive
,) unsigned long start
,
696 int count
, void* buf
, const bool write
)
698 #ifndef HAVE_MULTIDRIVE
703 unsigned long response
;
704 bool aligned
= !((uintptr_t)buf
& (CACHEALIGN_SIZE
- 1));
709 if (card_info
[drive
].initialized
<= 0)
711 ret
= sd_init_card(drive
);
712 if (!(card_info
[drive
].initialized
))
713 goto sd_transfer_error_nodma
;
716 if(count
< 0) /* XXX: why is it signed ? */
719 goto sd_transfer_error_nodma
;
721 if((start
+count
) > card_info
[drive
].numblocks
)
724 goto sd_transfer_error_nodma
;
727 /* skip SanDisk OF */
728 if (drive
== INTERNAL_AS3525
)
729 start
+= AMS_OF_SIZE
;
731 last_disk_activity
= current_tick
;
738 clean_dcache_range(buf
, count
* SECTOR_SIZE
);
740 dump_dcache_range(buf
, count
* SECTOR_SIZE
);
745 /* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH
746 * register, so we have to transfer maximum 127 sectors at a time. */
747 unsigned int transfer
= (count
>= 128) ? 127 : count
; /* sectors */
750 write
? SD_WRITE_MULTIPLE_BLOCK
: SD_READ_MULTIPLE_BLOCK
;
751 unsigned long bank_start
= start
;
752 unsigned long status
;
754 /* Only switch banks for internal storage */
755 if(drive
== INTERNAL_AS3525
)
757 unsigned int bank
= 0;
758 while(bank_start
>= BLOCKS_PER_BANK
)
760 bank_start
-= BLOCKS_PER_BANK
;
764 /* Switch bank if needed */
765 if(card_info
[INTERNAL_AS3525
].current_bank
!= bank
)
767 ret
= sd_select_bank(bank
);
771 goto sd_transfer_error
;
775 /* Do not cross a bank boundary in a single transfer loop */
776 if((transfer
+ bank_start
) > BLOCKS_PER_BANK
)
777 transfer
= BLOCKS_PER_BANK
- bank_start
;
780 /* Set bank_start to the correct unit (blocks or bytes) */
781 if(!(card_info
[drive
].ocr
& (1<<30))) /* not SDHC */
782 bank_start
*= SD_BLOCK_SIZE
;
786 dma_buf
= AS3525_PHYSICAL_ADDR(buf
);
790 dma_buf
= aligned_buffer
;
791 if(transfer
> UNALIGNED_NUM_SECTORS
)
792 transfer
= UNALIGNED_NUM_SECTORS
;
795 memcpy(uncached_buffer
, buf
, transfer
* SD_BLOCK_SIZE
);
798 ret
= sd_wait_for_tran_state(drive
);
802 goto sd_transfer_error
;
805 if(!send_cmd(drive
, cmd
, bank_start
, MCI_RESP
, &response
))
808 goto sd_transfer_error
;
813 dma_enable_channel(0, dma_buf
, MCI_FIFO(drive
),
814 (drive
== INTERNAL_AS3525
) ? DMA_PERI_SD
: DMA_PERI_SD_SLOT
,
815 DMAC_FLOWCTRL_PERI_MEM_TO_PERI
, true, false, 0, DMA_S8
, NULL
);
817 /*Small delay for writes prevents data crc failures at lower freqs*/
818 #ifdef HAVE_MULTIDRIVE
819 if((drive
== SD_SLOT_AS3525
) && !hs_card
)
821 int write_delay
= 125;
822 while(write_delay
--);
827 dma_enable_channel(0, MCI_FIFO(drive
), dma_buf
,
828 (drive
== INTERNAL_AS3525
) ? DMA_PERI_SD
: DMA_PERI_SD_SLOT
,
829 DMAC_FLOWCTRL_PERI_PERI_TO_MEM
, false, true, 0, DMA_S8
, NULL
);
831 MCI_DATA_TIMER(drive
) = write
?
832 SD_MAX_WRITE_TIMEOUT
: SD_MAX_READ_TIMEOUT
;
833 MCI_DATA_LENGTH(drive
) = transfer
* SD_BLOCK_SIZE
;
834 MCI_DATA_CTRL(drive
) = (1<<0) /* enable */ |
835 (!write
<<1) /* transfer direction */ |
837 (9<<4) /* 2^9 = 512 */ ;
839 /* Wakeup signal from NAND/MCIO isr on MCI_DATA_ERROR | MCI_DATA_END */
840 wakeup_wait(&transfer_completion_signal
, TIMEOUT_BLOCK
);
842 /* Wait for FIFO to empty, card may still be in PRG state for writes */
843 while(MCI_STATUS(drive
) & MCI_TX_ACTIVE
);
846 * If the write aborted early due to a tx underrun, disable the
847 * dma channel here, otherwise there are still 4 words in the fifo
848 * and the retried write will get corrupted.
850 dma_disable_channel(0);
852 last_disk_activity
= current_tick
;
854 if(!send_cmd(drive
, SD_STOP_TRANSMISSION
, 0, MCI_RESP
, &status
))
857 goto sd_transfer_error
;
860 if(!transfer_error
[drive
])
862 if(!write
&& !aligned
)
863 memcpy(buf
, uncached_buffer
, transfer
* SD_BLOCK_SIZE
);
864 buf
+= transfer
* SD_BLOCK_SIZE
;
867 loops
= 0; /* reset errors counter */
869 else if(loops
++ > PL180_MAX_TRANSFER_ERRORS
)
870 panicf("SD Xfer %s err:0x%x Disk%d", (write
? "write": "read"),
871 transfer_error
[drive
], drive
);
878 sd_transfer_error_nodma
:
884 card_info
[drive
].initialized
= 0;
889 int sd_read_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
895 ret
= sd_transfer_sectors(IF_MD2(drive
,) start
, count
, buf
, false);
896 mutex_unlock(&sd_mtx
);
901 int sd_write_sectors(IF_MD2(int drive
,) unsigned long start
, int count
,
905 unsigned long saved_start
= start
;
906 int saved_count
= count
;
907 void *saved_buf
= (void*)buf
;
913 ret
= sd_transfer_sectors(IF_MD2(drive
,) start
, count
, (void*)buf
, true);
916 if (ret
) /* write failed, no point in verifying */
923 int transfer
= count
;
924 if(transfer
> UNALIGNED_NUM_SECTORS
)
925 transfer
= UNALIGNED_NUM_SECTORS
;
927 sd_transfer_sectors(IF_MD2(drive
,) start
, transfer
, aligned_buffer
, false);
928 if (memcmp(buf
, aligned_buffer
, transfer
* 512) != 0) {
929 /* try the write again in the hope to repair the damage */
930 sd_transfer_sectors(IF_MD2(drive
,) saved_start
, saved_count
, saved_buf
, true);
931 panicf("sd: verify failed: sec=%ld n=%d!", start
, transfer
);
934 buf
+= transfer
* 512;
941 mutex_unlock(&sd_mtx
);
946 long sd_last_disk_activity(void)
948 return last_disk_activity
;
951 void sd_enable(bool on
)
953 #if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE)
954 extern int buttonlight_is_on
;
957 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
958 static bool cpu_boosted
= false;
961 if (sd_enabled
== on
)
962 return; /* nothing to do */
968 #if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE)
969 /* buttonlight AMSes need a bit of special handling for the buttonlight
970 * here due to the dual mapping of GPIOD and XPD */
971 CCU_IO
|= (1<<2); /* XPD is SD-MCI interface (b3:2 = 01) */
972 if (buttonlight_is_on
)
973 GPIOD_DIR
&= ~(1<<7);
978 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
979 if(card_detect_target()) /* If SD card present Boost cpu for voltage */
984 #endif /* defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE) */
988 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
994 #endif /* defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE) */
996 #if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE)
997 CCU_IO
&= ~(1<<2); /* XPD is general purpose IO (b3:2 = 00) */
998 if (buttonlight_is_on
)
1004 tCardInfo
*card_get_info_target(int card_no
)
1006 return &card_info
[card_no
];
1009 #ifdef CONFIG_STORAGE_MULTI
1010 int sd_num_drives(int first_drive
)
1012 /* We don't care which logical drive number(s) we have been assigned */
1017 #endif /* CONFIG_STORAGE_MULTI */