summaryrefslogtreecommitdiff
path: root/firmware/target/arm/as3525/sd-as3525.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/as3525/sd-as3525.c')
-rw-r--r--firmware/target/arm/as3525/sd-as3525.c138
1 files changed, 103 insertions, 35 deletions
diff --git a/firmware/target/arm/as3525/sd-as3525.c b/firmware/target/arm/as3525/sd-as3525.c
index 494a76a782..9e4a86acbe 100644
--- a/firmware/target/arm/as3525/sd-as3525.c
+++ b/firmware/target/arm/as3525/sd-as3525.c
@@ -89,6 +89,8 @@
89 | MCI_CMD_CRC_FAIL) 89 | MCI_CMD_CRC_FAIL)
90 90
91#define MCI_FIFO(i) ((unsigned long *) (pl180_base[i]+0x80)) 91#define MCI_FIFO(i) ((unsigned long *) (pl180_base[i]+0x80))
92
93#define IDE_INTERFACE_CLK (1<<6) /* non AHB interface */
92/* volumes */ 94/* volumes */
93#define INTERNAL_AS3525 0 /* embedded SD card */ 95#define INTERNAL_AS3525 0 /* embedded SD card */
94#define SD_SLOT_AS3525 1 /* SD slot if present */ 96#define SD_SLOT_AS3525 1 /* SD slot if present */
@@ -109,7 +111,8 @@ static void init_pl180_controller(const int drive);
109 111
110static tCardInfo card_info[NUM_DRIVES]; 112static tCardInfo card_info[NUM_DRIVES];
111 113
112/* maximum timeouts recommanded in the SD Specification v2.00 */ 114/* maximum timeouts recommended in the SD Specification v2.00 */
115/* MCI_DATA_TIMER register data timeout in card bus clock periods */
113#define SD_MAX_READ_TIMEOUT ((AS3525_PCLK_FREQ) / 1000 * 100) /* 100 ms */ 116#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 */ 117#define SD_MAX_WRITE_TIMEOUT ((AS3525_PCLK_FREQ) / 1000 * 250) /* 250 ms */
115 118
@@ -143,7 +146,17 @@ static unsigned char *uncached_buffer = AS3525_UNCACHED_ADDR(&aligned_buffer[0])
143 146
144static inline void mci_delay(void) { udelay(1000) ; } 147static inline void mci_delay(void) { udelay(1000) ; }
145 148
146static void enable_controller(bool on) 149static inline bool card_detect_target(void)
150{
151#if defined(HAVE_MULTIDRIVE)
152 return !(GPIOA_PIN(2));
153#else
154 return false;
155#endif
156}
157
158#if defined(HAVE_MULTIDRIVE) || defined(HAVE_HOTSWAP)
159static void enable_controller_mci(bool on)
147{ 160{
148 161
149#if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE) 162#if defined(HAVE_BUTTON_LIGHT) && defined(HAVE_MULTIDRIVE)
@@ -197,17 +210,33 @@ static void enable_controller(bool on)
197#endif 210#endif
198 } 211 }
199} 212}
213#endif /* defined(HAVE_MULTIDRIVE) || defined(HAVE_HOTSWAP) */
200 214
201static inline bool card_detect_target(void) 215/* AMS v1 have two different drive interfaces MCI_SD(XPD) and GGU_IDE */
216static void enable_controller(bool on, const int drive)
202{ 217{
203#if defined(HAVE_MULTIDRIVE) 218
204 return !(GPIOA_PIN(2)); 219 if (drive == INTERNAL_AS3525)
205#else 220 {
206 return false; 221#ifndef BOOTLOADER
222 if (on)
223 {
224 bitset32(&CGU_PERI, CGU_NAF_CLOCK_ENABLE);
225 CGU_IDE |= IDE_INTERFACE_CLK; /* interface enable */
226 }
227 else
228 {
229 CGU_IDE &= ~(IDE_INTERFACE_CLK); /* interface disable */
230 bitclr32(&CGU_PERI, CGU_NAF_CLOCK_ENABLE);
231 }
232#endif
233 }
234#if defined(HAVE_MULTIDRIVE) || defined(HAVE_HOTSWAP)
235 else
236 enable_controller_mci(on);
207#endif 237#endif
208} 238}
209 239
210
211#ifdef HAVE_HOTSWAP 240#ifdef HAVE_HOTSWAP
212static int sd1_oneshot_callback(struct timeout *tmo) 241static int sd1_oneshot_callback(struct timeout *tmo)
213{ 242{
@@ -326,6 +355,7 @@ static bool send_cmd(const int drive, const int cmd, const int arg,
326 return false; 355 return false;
327} 356}
328 357
358/* MCI_CLOCK = MCLK / 2x(ClkDiv[bits 7:0]+1) */
329#define MCI_FULLSPEED (MCI_CLOCK_ENABLE | MCI_CLOCK_BYPASS) /* MCLK */ 359#define MCI_FULLSPEED (MCI_CLOCK_ENABLE | MCI_CLOCK_BYPASS) /* MCLK */
330#define MCI_HALFSPEED (MCI_CLOCK_ENABLE) /* MCLK/2 */ 360#define MCI_HALFSPEED (MCI_CLOCK_ENABLE) /* MCLK/2 */
331#define MCI_QUARTERSPEED (MCI_CLOCK_ENABLE | 1) /* MCLK/4 */ 361#define MCI_QUARTERSPEED (MCI_CLOCK_ENABLE | 1) /* MCLK/4 */
@@ -345,7 +375,7 @@ static int sd_init_card(const int drive)
345 /* 100 - 400kHz clock required for Identification Mode */ 375 /* 100 - 400kHz clock required for Identification Mode */
346 /* Start of Card Identification Mode ************************************/ 376 /* Start of Card Identification Mode ************************************/
347 377
348 /* CMD0 Go Idle */ 378 /* CMD0 Go Idle -- all card functions switch back to default */
349 if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL)) 379 if(!send_cmd(drive, SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
350 return -1; 380 return -1;
351 mci_delay(); 381 mci_delay();
@@ -393,10 +423,10 @@ static int sd_init_card(const int drive)
393 423
394 if(sd_wait_for_tran_state(drive)) 424 if(sd_wait_for_tran_state(drive))
395 return -6; 425 return -6;
396 /* CMD6 */ 426 /* CMD6 0xf indicates no influence, [3:0],0x1 - HS Access*/
397 if(!send_cmd(drive, SD_SWITCH_FUNC, 0x80fffff1, MCI_NO_RESP, NULL)) 427 if(!send_cmd(drive, SD_SWITCH_FUNC, 0x80fffff1, MCI_NO_RESP, NULL))
398 return -7; 428 return -7;
399 sleep(HZ/10); 429 sleep(HZ/10);/* need to wait at least 8 clock periods */
400 430
401 /* go back to STBY state so we can read csd */ 431 /* go back to STBY state so we can read csd */
402 /* CMD7 w/rca=0: Deselect card to put it in STBY state */ 432 /* CMD7 w/rca=0: Deselect card to put it in STBY state */
@@ -517,7 +547,7 @@ static void init_pl180_controller(const int drive)
517int sd_init(void) 547int sd_init(void)
518{ 548{
519 int ret; 549 int ret;
520 CGU_IDE = (1<<6) /* enable non AHB interface*/ 550 CGU_IDE = IDE_INTERFACE_CLK /* enable interface */
521 | (AS3525_IDE_DIV << 2) 551 | (AS3525_IDE_DIV << 2)
522 | AS3525_CLK_PLLA; /* clock source = PLLA */ 552 | AS3525_CLK_PLLA; /* clock source = PLLA */
523 553
@@ -540,7 +570,9 @@ int sd_init(void)
540 /* init mutex */ 570 /* init mutex */
541 mutex_init(&sd_mtx); 571 mutex_init(&sd_mtx);
542 572
543 enable_controller(false); 573 for (int i = 0; i < NUM_DRIVES ; i++)
574 enable_controller(false, i);
575
544 return 0; 576 return 0;
545} 577}
546 578
@@ -656,7 +688,7 @@ static int sd_transfer_sectors(IF_MD(int drive,) unsigned long start,
656 unsigned long response; 688 unsigned long response;
657 bool aligned = !((uintptr_t)buf & (CACHEALIGN_SIZE - 1)); 689 bool aligned = !((uintptr_t)buf & (CACHEALIGN_SIZE - 1));
658 690
659 enable_controller(true); 691 enable_controller(true, drive);
660 led(true); 692 led(true);
661 693
662 if (card_info[drive].initialized <= 0) 694 if (card_info[drive].initialized <= 0)
@@ -692,27 +724,21 @@ static int sd_transfer_sectors(IF_MD(int drive,) unsigned long start,
692 else 724 else
693 discard_dcache_range(buf, count * SECTOR_SIZE); 725 discard_dcache_range(buf, count * SECTOR_SIZE);
694 } 726 }
695 727 const int cmd = write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK;
696 while(count) 728 while(count > 0)
697 { 729 {
698 /* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH 730 /* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH
699 * register, so we have to transfer maximum 127 sectors at a time. */ 731 * register, so we have to transfer maximum 127 sectors at a time. */
700 unsigned int transfer = (count >= 128) ? 127 : count; /* sectors */ 732 unsigned int transfer = (count >= 128) ? 127 : count; /* sectors */
701 void *dma_buf; 733 void *dma_buf;
702 const int cmd = 734
703 write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK;
704 unsigned long bank_start = start; 735 unsigned long bank_start = start;
705 unsigned long status;
706 736
707 /* Only switch banks for internal storage */ 737 /* Only switch banks for internal storage */
708 if(drive == INTERNAL_AS3525) 738 if(drive == INTERNAL_AS3525)
709 { 739 {
710 unsigned int bank = 0; 740 unsigned int bank = bank_start / BLOCKS_PER_BANK;
711 while(bank_start >= BLOCKS_PER_BANK) 741 bank_start -= bank * BLOCKS_PER_BANK;
712 {
713 bank_start -= BLOCKS_PER_BANK;
714 bank++;
715 }
716 742
717 /* Switch bank if needed */ 743 /* Switch bank if needed */
718 if(card_info[INTERNAL_AS3525].current_bank != bank) 744 if(card_info[INTERNAL_AS3525].current_bank != bank)
@@ -770,10 +796,7 @@ static int sd_transfer_sectors(IF_MD(int drive,) unsigned long start,
770 /*Small delay for writes prevents data crc failures at lower freqs*/ 796 /*Small delay for writes prevents data crc failures at lower freqs*/
771#ifdef HAVE_MULTIDRIVE 797#ifdef HAVE_MULTIDRIVE
772 if((drive == SD_SLOT_AS3525) && !hs_card) 798 if((drive == SD_SLOT_AS3525) && !hs_card)
773 { 799 udelay(4);
774 int write_delay = 125;
775 while(write_delay--);
776 }
777#endif 800#endif
778 } 801 }
779 else 802 else
@@ -804,7 +827,7 @@ static int sd_transfer_sectors(IF_MD(int drive,) unsigned long start,
804 827
805 last_disk_activity = current_tick; 828 last_disk_activity = current_tick;
806 829
807 if(!send_cmd(drive, SD_STOP_TRANSMISSION, 0, MCI_RESP, &status)) 830 if(!send_cmd(drive, SD_STOP_TRANSMISSION, 0, MCI_RESP, &response))
808 { 831 {
809 ret = -4*20; 832 ret = -4*20;
810 goto sd_transfer_error; 833 goto sd_transfer_error;
@@ -831,7 +854,7 @@ sd_transfer_error:
831sd_transfer_error_nodma: 854sd_transfer_error_nodma:
832 855
833 led(false); 856 led(false);
834 enable_controller(false); 857 enable_controller(false, drive);
835 858
836 if (ret) /* error */ 859 if (ret) /* error */
837 card_info[drive].initialized = 0; 860 card_info[drive].initialized = 0;
@@ -922,12 +945,18 @@ void ams_sd_get_debug_info(struct ams_sd_debug_info *info)
922 #define MCI_SD *((volatile unsigned long *)(SD_MCI_BASE + 0x04)) 945 #define MCI_SD *((volatile unsigned long *)(SD_MCI_BASE + 0x04))
923 946
924 mutex_lock(&sd_mtx); 947 mutex_lock(&sd_mtx);
925 enable_controller(true); /* must be on to read regs */ 948
949 for (int i = 0; i < NUM_DRIVES ; i++)
950 enable_controller(true, i); /* must be on to read regs */
951
926 info->mci_nand = MCI_NAND; 952 info->mci_nand = MCI_NAND;
927#ifdef HAVE_MULTIDRIVE 953#ifdef HAVE_MULTIDRIVE
928 info->mci_sd = MCI_SD; 954 info->mci_sd = MCI_SD;
929#endif 955#endif
930 enable_controller(false); 956
957 for (int i = 0; i < NUM_DRIVES ; i++)
958 enable_controller(false, i);
959
931 mutex_unlock(&sd_mtx); 960 mutex_unlock(&sd_mtx);
932} 961}
933 962
@@ -948,10 +977,10 @@ int sd_event(long id, intptr_t data)
948 977
949 if (id == SYS_HOTSWAP_INSERTED) 978 if (id == SYS_HOTSWAP_INSERTED)
950 { 979 {
951 enable_controller(true); 980 enable_controller(true, data);
952 init_pl180_controller(data); 981 init_pl180_controller(data);
953 rc = sd_init_card(data); 982 rc = sd_init_card(data);
954 enable_controller(false); 983 enable_controller(false, data);
955 } 984 }
956 985
957 mutex_unlock(&sd_mtx); 986 mutex_unlock(&sd_mtx);
@@ -968,3 +997,42 @@ int sd_event(long id, intptr_t data)
968 997
969 return rc; 998 return rc;
970} 999}
1000
1001#if defined(CONFIG_POWER_SAVING) && (CONFIG_POWER_SAVING & POWERSV_DISK)
1002/* declared in system-as3525.c */
1003void ams_sd_set_low_speed(bool slow)
1004{
1005 /* block access while speed is changed */
1006 mutex_lock(&sd_mtx);
1007 enable_controller(true, INTERNAL_AS3525);
1008
1009 /* After a data write, data cannot be written to MCI_CLOCK
1010 for 3 MCLK periods + 2 PCLK periods. ~10us worst case
1011 */
1012 udelay(100);
1013 if (slow)
1014 {
1015 /* only affects internal drive clock speed*/
1016 CGU_IDE = (CGU_IDE & ~(0xF << 2)) | (AS3525_IDE_DIV_MAX << 2);
1017 /* power save is enabled for the sd card(s) */
1018 for (int i = 0; i < NUM_DRIVES ; i++)
1019 {
1020 if (i != INTERNAL_AS3525 && (MCI_CLOCK(i) & MCI_CLOCK_POWERSAVE) == 0)
1021 MCI_CLOCK(i) |= MCI_CLOCK_POWERSAVE;
1022 }
1023 }
1024 else
1025 {
1026 /* Full Speed */
1027 CGU_IDE = (CGU_IDE & ~(0xF << 2)) | (AS3525_IDE_DIV << 2);
1028 for (int i = 0; i < NUM_DRIVES ; i++)
1029 {
1030 if (i != INTERNAL_AS3525 && (MCI_CLOCK(i) & MCI_CLOCK_POWERSAVE) != 0)
1031 MCI_CLOCK(i) = (MCI_CLOCK(i) & ~MCI_CLOCK_POWERSAVE);
1032 }
1033 }
1034 enable_controller(false, INTERNAL_AS3525);
1035 mutex_unlock(&sd_mtx);
1036}
1037#endif
1038