AMS SoC's: Some register bit changes need interrupt protection: timer API and CGU_PERI.
[kugel-rb.git] / firmware / target / arm / as3525 / sd-as3525.c
blob6e5845cf0a3289ee0941a8cf83562e37d9e3e209
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 */
26 #include "fat.h"
27 #include "thread.h"
28 #include "led.h"
29 #include "sdmmc.h"
30 #include "system.h"
31 #include "cpu.h"
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "as3525.h"
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"
40 #include "panic.h"
41 #ifdef HAVE_BUTTON_LIGHT
42 #include "backlight-target.h"
43 #endif
44 #include "stdbool.h"
45 #include "ata_idle_notify.h"
46 #include "sd.h"
47 #include "usb.h"
48 /*#define LOGF_ENABLE*/
49 #include "logf.h"
51 #ifdef HAVE_HOTSWAP
52 #include "disk.h"
53 #endif
55 #define VERIFY_WRITE 1
57 /* command flags */
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 \
86 ( MCI_DATA_CRC_FAIL \
87 | MCI_DATA_TIMEOUT \
88 | MCI_TX_UNDERRUN \
89 | MCI_RX_OVERRUN \
90 | MCI_START_BIT_ERR)
92 #define MCI_RESPONSE_ERROR \
93 ( MCI_CMD_TIMEOUT \
94 | MCI_CMD_CRC_FAIL)
96 #define MCI_FIFO(i) ((unsigned long *) (pl180_base[i]+0x80))
97 /* volumes */
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] = {
102 NAND_FLASH_BASE
103 #ifdef HAVE_MULTIDRIVE
104 , SD_MCI_BASE
105 #endif
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)
136 #endif
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));
154 #else
155 return false;
156 #endif
160 #ifdef HAVE_HOTSWAP
161 static int sd1_oneshot_callback(struct timeout *tmo)
163 (void)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);
171 else
172 queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
174 return 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 */
187 void INT_NAND(void)
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
198 void INT_MCI0(void)
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;
207 #endif
209 static bool send_cmd(const int drive, const int cmd, const int arg,
210 const int flags, long *response)
212 int status;
214 unsigned cmd_retries = 6;
215 while(cmd_retries--)
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))
219 return false;
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 */
228 MCI_COMMAND(drive) =
229 /*b0:5*/ cmd
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) &&
249 (flags & MCI_NOCRC))
250 break;
251 logf("sd cmd error: drive %d cmd %d arg %08x sd_status %08x resp0 %08lx",
252 drive, cmd, arg, status, response[0]);
253 continue;
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);
271 return true;
274 else if(status & MCI_CMD_SENT) /* CMD sent, no response required */
275 return true;
278 return false;
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;
289 long init_timeout;
290 bool sd_v2 = false;
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 ************************************/
300 /* CMD0 Go Idle */
301 if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
302 return -1;
303 mci_delay();
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)
309 sd_v2 = true;
311 /* timeout for initialization is 1sec, from SD Specification 2.00 */
312 init_timeout = current_tick + HZ;
314 do {
315 /* this timeout is the only valid error for this loop*/
316 if(TIME_AFTER(current_tick, init_timeout))
317 return -2;
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)));
325 /* CMD2 send CID */
326 if(!send_cmd(drive, SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP,
327 card_info[drive].cid))
328 return -3;
330 /* CMD3 send RCA */
331 if(!send_cmd(drive, SD_SEND_RELATIVE_ADDR, 0, MCI_RESP,
332 &card_info[drive].rca))
333 return -4;
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 */
340 if(sd_v2)
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))
344 return -5;
346 if(sd_wait_for_tran_state(drive))
347 return -6;
348 /* CMD6 */
349 if(!send_cmd(drive, SD_SWITCH_FUNC, 0x80fffff1, MCI_NO_RESP, NULL))
350 return -7;
351 mci_delay();
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))
356 return -8;
357 mci_delay();
359 #endif /* HAVE_MULTIDRIVE */
361 /* CMD9 send CSD */
362 if(!send_cmd(drive, SD_SEND_CSD, card_info[drive].rca,
363 MCI_RESP|MCI_LONG_RESP, card_info[drive].csd))
364 return -9;
366 sd_parse_csd(&card_info[drive]);
368 #if defined(HAVE_MULTIDRIVE)
369 hs_card = (card_info[drive].speed == 50000000);
370 #endif
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)
376 else
377 /* MCICLK = PCLK/2 = 31MHz(HS) or PCLK/4 = 15.5 Mhz (STD)*/
378 MCI_CLOCK(drive) = (hs_card ? MCI_HALFSPEED : MCI_QUARTERSPEED);
379 #endif
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))
383 return -10;
385 /* Switch to to 4 bit widebus mode */
386 if(sd_wait_for_tran_state(drive) < 0)
387 return -11;
388 /* ACMD42 */
389 if(!send_cmd(drive, SD_SET_CLR_CARD_DETECT, 0, MCI_ACMD|MCI_RESP, &response))
390 return -15;
391 /* ACMD6 */
392 if(!send_cmd(drive, SD_SET_BUS_WIDTH, 2, MCI_ACMD|MCI_RESP, &response))
393 return -13;
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
401 * CSD register)
403 if(drive == INTERNAL_AS3525)
405 const int ret = sd_select_bank(-1);
406 if(ret < 0)
407 return ret -16;
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))
411 return -17;
412 mci_delay();
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))
417 return -18;
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))
425 return -19;
428 card_info[drive].initialized = 1;
430 return 0;
433 static void sd_thread(void) __attribute__((noreturn));
434 static void sd_thread(void)
436 struct queue_event ev;
437 bool idle_notified = false;
439 while (1)
441 queue_wait_w_tmo(&sd_queue, &ev, HZ);
443 switch ( ev.id )
445 #ifdef HAVE_HOTSWAP
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
453 another thread */
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)
469 sd_enable(true);
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
482 if (microsd_init)
483 queue_broadcast(SYS_FS_CHANGED, 0);
485 /* Access is now safe */
486 mutex_unlock(&sd_mtx);
487 fat_unlock();
488 sd_enable(false);
490 break;
491 #endif
492 case SYS_TIMEOUT:
493 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
495 idle_notified = false;
497 else
499 /* never let a timer wrap confuse us */
500 next_yield = current_tick;
502 if (!idle_notified)
504 call_storage_idle_notifys(false);
505 idle_notified = true;
508 break;
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);
515 break;
516 case SYS_USB_DISCONNECTED:
517 usb_acknowledge(SYS_USB_DISCONNECTED_ACK);
518 break;
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
531 VIC_INT_ENABLE =
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;
542 #else
543 VIC_INT_ENABLE = INTERRUPT_NAND;
544 #endif
546 MCI_POWER(drive) = MCI_POWER_UP | (MCI_VDD_3_0); /* OF Setting */
547 mci_delay();
549 MCI_POWER(drive) |= MCI_POWER_ON;
550 mci_delay();
552 MCI_SELECT(drive) = 0;
554 /* Pl180 clocks get turned on at start of card init */
557 int sd_init(void)
559 int ret;
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 */
568 CCU_IO |= (1<<2);
569 #endif
571 wakeup_init(&transfer_completion_signal);
573 init_pl180_controller(INTERNAL_AS3525);
574 ret = sd_init_card(INTERNAL_AS3525);
575 if(ret < 0)
576 return ret;
577 #ifdef HAVE_MULTIDRIVE
578 init_pl180_controller(SD_SLOT_AS3525);
579 #endif
581 /* init mutex */
582 mutex_init(&sd_mtx);
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));
588 sd_enabled = true;
589 sd_enable(false);
591 return 0;
594 #ifdef HAVE_HOTSWAP
595 bool sd_removable(IF_MD_NONVOID(int drive))
597 return (drive==1);
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;
611 while (1)
613 if(!send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca, MCI_RESP,
614 &response))
615 return -1;
617 if (((response >> 9) & 0xf) == SD_TRAN)
618 return 0;
620 if(TIME_AFTER(current_tick, timeout))
621 return -2;
623 if (TIME_AFTER(current_tick, next_yield))
625 yield();
626 next_yield = current_tick + MIN_YIELD_PERIOD;
631 static int sd_select_bank(signed char bank)
633 int ret;
634 unsigned loops = 0;
636 memset(uncached_buffer, 0, 512);
637 if(bank == -1)
638 { /* enable bank switching */
639 uncached_buffer[0] = 16;
640 uncached_buffer[1] = 1;
641 uncached_buffer[2] = 10;
643 else
644 uncached_buffer[0] = bank;
646 do {
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);
652 if (ret < 0)
653 return ret - 2;
655 if(!send_cmd(INTERNAL_AS3525, SD_SWITCH_FUNC, 0x80ffffef, MCI_NO_RESP,
656 NULL))
657 return -1;
659 mci_delay();
661 if(!send_cmd(INTERNAL_AS3525, 35, 0, MCI_NO_RESP, NULL))
662 return -2;
664 mci_delay();
666 dma_retain();
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,
671 NULL);
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 */ |
677 (1<<3) /* DMA */ |
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 );
686 dma_release();
688 } while(transfer_error[INTERNAL_AS3525]);
690 card_info[INTERNAL_AS3525].current_bank = (bank == -1) ? 0 : bank;
692 return 0;
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
699 const int drive = 0;
700 #endif
701 int ret = 0;
702 unsigned loops = 0;
703 unsigned long response;
704 bool aligned = !((uintptr_t)buf & (CACHEALIGN_SIZE - 1));
706 sd_enable(true);
707 led(true);
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 ? */
718 ret = -20;
719 goto sd_transfer_error_nodma;
721 if((start+count) > card_info[drive].numblocks)
723 ret = -21;
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;
733 dma_retain();
735 if(aligned)
737 if(write)
738 clean_dcache_range(buf, count * SECTOR_SIZE);
739 else
740 dump_dcache_range(buf, count * SECTOR_SIZE);
743 while(count)
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 */
748 void *dma_buf;
749 const int cmd =
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;
761 bank++;
764 /* Switch bank if needed */
765 if(card_info[INTERNAL_AS3525].current_bank != bank)
767 ret = sd_select_bank(bank);
768 if (ret < 0)
770 ret -= 20;
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;
784 if(aligned)
786 dma_buf = AS3525_PHYSICAL_ADDR(buf);
788 else
790 dma_buf = aligned_buffer;
791 if(transfer > UNALIGNED_NUM_SECTORS)
792 transfer = UNALIGNED_NUM_SECTORS;
794 if(write)
795 memcpy(uncached_buffer, buf, transfer * SD_BLOCK_SIZE);
798 ret = sd_wait_for_tran_state(drive);
799 if (ret < 0)
801 ret -= 2*20;
802 goto sd_transfer_error;
805 if(!send_cmd(drive, cmd, bank_start, MCI_RESP, &response))
807 ret -= 3*20;
808 goto sd_transfer_error;
811 if(write)
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--);
824 #endif
826 else
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 */ |
836 (1<<3) /* DMA */ |
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))
856 ret = -4*20;
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;
865 start += transfer;
866 count -= transfer;
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);
874 sd_transfer_error:
876 dma_release();
878 sd_transfer_error_nodma:
880 led(false);
881 sd_enable(false);
883 if (ret) /* error */
884 card_info[drive].initialized = 0;
886 return ret;
889 int sd_read_sectors(IF_MD2(int drive,) unsigned long start, int count,
890 void* buf)
892 int ret;
894 mutex_lock(&sd_mtx);
895 ret = sd_transfer_sectors(IF_MD2(drive,) start, count, buf, false);
896 mutex_unlock(&sd_mtx);
898 return ret;
901 int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
902 const void* buf)
904 #ifdef VERIFY_WRITE
905 unsigned long saved_start = start;
906 int saved_count = count;
907 void *saved_buf = (void*)buf;
908 #endif
909 int ret;
911 mutex_lock(&sd_mtx);
913 ret = sd_transfer_sectors(IF_MD2(drive,) start, count, (void*)buf, true);
915 #ifdef VERIFY_WRITE
916 if (ret) /* write failed, no point in verifying */
917 goto write_error;
919 count = saved_count;
920 buf = saved_buf;
921 start = saved_start;
922 while (count) {
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;
935 count -= transfer;
936 start += transfer;
938 #endif
940 write_error:
941 mutex_unlock(&sd_mtx);
943 return ret;
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;
955 #endif
957 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
958 static bool cpu_boosted = false;
959 #endif
961 if (sd_enabled == on)
962 return; /* nothing to do */
964 sd_enabled = on;
966 if(on)
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);
974 else
975 _buttonlight_off();
976 #endif
978 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
979 if(card_detect_target()) /* If SD card present Boost cpu for voltage */
981 cpu_boosted = true;
982 cpu_boost(true);
984 #endif /* defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE) */
986 else
988 #if defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE)
989 if(cpu_boosted)
991 cpu_boost(false);
992 cpu_boosted = false;
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)
999 _buttonlight_on();
1000 #endif
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 */
1013 (void)first_drive;
1015 return NUM_DRIVES;
1017 #endif /* CONFIG_STORAGE_MULTI */