Subversion Repositories freemyipod

Rev

Rev 920 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 920 Rev 965
Line 66... Line 66...
66
uint16_t (*ata_bbt)[0x20];
66
uint16_t (*ata_bbt)[0x20];
67
uint64_t ata_virtual_sectors;
67
uint64_t ata_virtual_sectors;
68
uint32_t ata_last_offset;
68
uint32_t ata_last_offset;
69
uint64_t ata_last_phys;
69
uint64_t ata_last_phys;
70
 
70
 
-
 
71
static uint16_t ata_read_cbr(uint32_t volatile* reg);
71
int ata_bbt_read_sectors(uint32_t sector, uint32_t count, void* buffer)
72
int ata_bbt_read_sectors(uint32_t sector, uint32_t count, void* buffer)
72
{
73
{
73
    if (ata_last_phys != sector - 1 && ata_last_phys > sector - 64) ata_soft_reset();
74
    if (ata_last_phys != sector - 1 && ata_last_phys > sector - 64) ata_reset();
74
    int rc = ata_rw_sectors_internal(sector, count, buffer, false);
75
    int rc = ata_rw_sectors_internal(sector, count, buffer, false);
75
    if (rc) rc = ata_rw_sectors_internal(sector, count, buffer, false);
76
    if (rc) rc = ata_rw_sectors_internal(sector, count, buffer, false);
76
    ata_last_phys = sector + count - 1;
77
    ata_last_phys = sector + count - 1;
77
    ata_last_offset = 0;
78
    ata_last_offset = 0;
78
    if (IS_ERR(rc))
79
    if (IS_ERR(rc))
Line 87... Line 88...
87
    .set_retries = ata_set_retries,
88
    .set_retries = ata_set_retries,
88
    .srst_after_error = ata_srst_after_error,
89
    .srst_after_error = ata_srst_after_error,
89
#ifdef ATA_HAVE_BBT
90
#ifdef ATA_HAVE_BBT
90
    .bbt_translate = ata_bbt_translate,
91
    .bbt_translate = ata_bbt_translate,
91
    .bbt_reload = ata_bbt_reload,
92
    .bbt_reload = ata_bbt_reload,
92
    .bbt_disable = ata_bbt_disable
93
    .bbt_disable = ata_bbt_disable,
93
#endif
94
#endif
94
};
95
};
95
 
96
 
96
 
97
 
97
void ata_set_retries(int retries)
98
void ata_set_retries(int retries)
Line 105... Line 106...
105
}
106
}
106
 
107
 
107
static uint16_t ata_read_cbr(uint32_t volatile* reg)
108
static uint16_t ata_read_cbr(uint32_t volatile* reg)
108
{
109
{
109
    while (!(ATA_PIO_READY & 2)) yield();
110
    while (!(ATA_PIO_READY & 2)) yield();
110
    volatile uint32_t dummy = *reg;
111
    uint32_t dummy = *reg;
111
    while (!(ATA_PIO_READY & 1)) yield();
112
    while (!(ATA_PIO_READY & 1)) yield();
112
    return ATA_PIO_RDATA;
113
    return ATA_PIO_RDATA;
113
}
114
}
114
 
115
 
115
static void ata_write_cbr(uint32_t volatile* reg, uint16_t data)
116
static void ata_write_cbr(uint32_t volatile* reg, uint16_t data)
Line 490... Line 491...
490
    SDCI_DCTRL = SDCI_DCTRL_TXFIFORST | SDCI_DCTRL_RXFIFORST;
491
    SDCI_DCTRL = SDCI_DCTRL_TXFIFORST | SDCI_DCTRL_RXFIFORST;
491
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_CEATA_RW_MULTIPLE_BLOCK)
492
    PASS_RC(mmc_send_command(SDCI_CMD_CMD_NUM(MMC_CMD_CEATA_RW_MULTIPLE_BLOCK)
492
                           | SDCI_CMD_CMD_TYPE_ADTC | cmdtype | responsetype
493
                           | SDCI_CMD_CMD_TYPE_ADTC | cmdtype | responsetype
493
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
494
                           | SDCI_CMD_RES_SIZE_48 | SDCI_CMD_NCR_NID_NCR,
494
                             direction | MMC_CMD_CEATA_RW_MULTIPLE_BLOCK_COUNT(count),
495
                             direction | MMC_CMD_CEATA_RW_MULTIPLE_BLOCK_COUNT(count),
495
                             NULL, CEATA_COMMAND_TIMEOUT), 4, 0);
496
                             NULL, CEATA_COMMAND_TIMEOUT), 3, 0);
496
    if (write) SDCI_DCTRL = SDCI_DCTRL_TRCONT_TX;
497
    if (write) SDCI_DCTRL = SDCI_DCTRL_TRCONT_TX;
497
    if (wakeup_wait(&mmc_wakeup, timeout) == THREAD_TIMEOUT)
498
    if (wakeup_wait(&mmc_wakeup, timeout) == THREAD_TIMEOUT)
498
    {
499
    {
499
        PASS_RC(ceata_cancel_command(), 4, 1);
500
        PASS_RC(ceata_cancel_command(), 3, 1);
500
        RET_ERR(2);
501
        RET_ERR(2);
501
    }
502
    }
502
    PASS_RC(mmc_dsta_check_data_success(), 4, 3);
503
    PASS_RC(mmc_dsta_check_data_success(), 3, 3);
503
    if (wakeup_wait(&mmc_comp_wakeup, timeout) == THREAD_TIMEOUT)
504
    if (wakeup_wait(&mmc_comp_wakeup, timeout) == THREAD_TIMEOUT)
504
    {
505
    {
505
        PASS_RC(ceata_cancel_command(), 4, 4);
506
        PASS_RC(ceata_cancel_command(), 3, 4);
506
        RET_ERR(4);
507
        RET_ERR(5);
507
    }
508
    }
508
    PASS_RC(ceata_check_error(), 4, 5);
509
    PASS_RC(ceata_check_error(), 3, 6);
509
    return 0;
510
    return 0;
510
}
511
}
511
 
512
 
512
int ata_identify(uint16_t* buf)
513
int ata_identify(uint16_t* buf)
513
{
514
{
Line 541... Line 542...
541
    return ata_powered;
542
    return ata_powered;
542
}
543
}
543
 
544
 
544
int ata_set_feature(uint32_t feature, uint32_t param)
545
int ata_set_feature(uint32_t feature, uint32_t param)
545
{
546
{
-
 
547
    if (ceata)
-
 
548
    {
-
 
549
        memset(ceata_taskfile, 0, 16);
-
 
550
        ceata_taskfile[0x1] = feature;
-
 
551
        ceata_taskfile[0x2] = param;
-
 
552
        ceata_taskfile[0xf] = 0xef;
-
 
553
        PASS_RC(ceata_wait_idle(), 2, 0);
-
 
554
        PASS_RC(ceata_write_multiple_register(0, ceata_taskfile, 16), 2, 1);
-
 
555
        PASS_RC(ceata_wait_idle(), 2, 2);
-
 
556
    }
-
 
557
    else
-
 
558
    {
546
    PASS_RC(ata_wait_for_rdy(500000), 1, 0);
559
        PASS_RC(ata_wait_for_rdy(2000000), 2, 0);
547
    ata_write_cbr(&ATA_PIO_DVR, 0);
560
        ata_write_cbr(&ATA_PIO_DVR, 0);
548
    ata_write_cbr(&ATA_PIO_FED, 3);
561
        ata_write_cbr(&ATA_PIO_FED, feature);
549
    ata_write_cbr(&ATA_PIO_SCR, param);
562
        ata_write_cbr(&ATA_PIO_SCR, param);
550
    ata_write_cbr(&ATA_PIO_CSD, feature);
563
        ata_write_cbr(&ATA_PIO_CSD, 0xef);
551
    PASS_RC(ata_wait_for_rdy(500000), 1, 1);
564
        PASS_RC(ata_wait_for_rdy(2000000), 2, 1);
-
 
565
    }
552
    return 0;
566
    return 0;
553
}
567
}
554
 
568
 
555
int ata_power_up()
569
int ata_power_up()
556
{
570
{
557
    ata_set_active();
571
    ata_set_active();
558
    if (ata_powered) return 0;
-
 
559
    i2c_sendbyte(0, 0xe6, 0x1b, 1);
572
    i2c_sendbyte(0, 0xe6, 0x1b, 1);
560
    if (ceata)
573
    if (ceata)
561
    {
574
    {
-
 
575
        ata_lba48 = true;
-
 
576
        ata_dma = true;
-
 
577
        PCON(8) = 0x33333333;
-
 
578
        PCON(9) = 0x00000033;
-
 
579
        PCON(11) |= 0xf;
-
 
580
        *((uint32_t volatile*)0x38a00000) = 0;
-
 
581
        *((uint32_t volatile*)0x38700000) = 0;
562
        clockgate_enable(9, true);
582
        clockgate_enable(9, true);
563
        SDCI_RESET = 0xa5;
583
        SDCI_RESET = 0xa5;
564
        sleep(1000);
584
        sleep(1000);
565
        *((uint32_t volatile*)0x3cf00380) = 0;
585
        *((uint32_t volatile*)0x3cf00380) = 0;
566
        *((uint32_t volatile*)0x3cf0010c) = 0xff;
586
        *((uint32_t volatile*)0x3cf0010c) = 0xff;
567
        SDCI_CTRL = SDCI_CTRL_SDCIEN | SDCI_CTRL_CLK_SEL_SDCLK
587
        SDCI_CTRL = SDCI_CTRL_SDCIEN | SDCI_CTRL_CLK_SEL_SDCLK
568
                  | SDCI_CTRL_BIT_8 | SDCI_CTRL_BIT_14;
588
                  | SDCI_CTRL_BIT_8 | SDCI_CTRL_BIT_14;
569
        SDCI_CDIV = SDCI_CDIV_CLKDIV(260);
589
        SDCI_CDIV = SDCI_CDIV_CLKDIV(260);
570
        *((uint32_t volatile*)0x3cf00200) = 0xb000f;
590
        *((uint32_t volatile*)0x3cf00200) = 0xb000f;
571
        SDCI_IRQ_MASK = SDCI_IRQ_MASK_MASK_DAT_DONE_INT | SDCI_IRQ_MASK_MASK_IOCARD_IRQ_INT;
591
        SDCI_IRQ_MASK = SDCI_IRQ_MASK_MASK_DAT_DONE_INT | SDCI_IRQ_MASK_MASK_IOCARD_IRQ_INT;
572
        PASS_RC(mmc_init(), 2, 0);
592
        PASS_RC(mmc_init(), 3, 0);
573
        SDCI_CDIV = SDCI_CDIV_CLKDIV(4);
593
        SDCI_CDIV = SDCI_CDIV_CLKDIV(4);
574
        sleep(10000);
594
        sleep(10000);
575
        PASS_RC(ceata_init(8), 2, 1);
595
        PASS_RC(ceata_init(8), 3, 1);
576
        PASS_RC(ata_identify(ata_identify_data), 2, 2);
596
        PASS_RC(ata_identify(ata_identify_data), 3, 2);
577
    }
597
    }
578
    else
598
    else
579
    {
599
    {
-
 
600
        PCON(7) = 0x44444444;
-
 
601
        PCON(8) = 0x44444444;
580
        sleep(1000);
602
        PCON(9) = 0x44444444;
-
 
603
        PCON(10) = (PCON(10) & ~0xffff) | 0x4444;
581
        clockgate_enable(5, true);
604
        clockgate_enable(5, true);
582
        ATA_CFG = BIT(0);
605
        ATA_CFG = BIT(0);
583
        sleep(1000);
606
        sleep(10000);
584
        ATA_CFG = 0;
607
        ATA_CFG = 0;
585
        sleep(6000);
608
        sleep(6000);
586
        ATA_SWRST = BIT(0);
609
        ATA_SWRST = BIT(0);
587
        sleep(500);
610
        sleep(500);
588
        ATA_SWRST = 0;
611
        ATA_SWRST = 0;
Line 591... Line 614...
591
        sleep(200000);
614
        sleep(200000);
592
        ATA_PIO_TIME = 0x191f7;
615
        ATA_PIO_TIME = 0x191f7;
593
        ATA_PIO_LHR = 0;
616
        ATA_PIO_LHR = 0;
594
        ATA_CFG = BIT(6);
617
        ATA_CFG = BIT(6);
595
        while (!(ATA_PIO_READY & BIT(1))) sleep(100);
618
        while (!(ATA_PIO_READY & BIT(1))) sleep(100);
596
        PASS_RC(ata_identify(ata_identify_data), 2, 0);
619
        PASS_RC(ata_identify(ata_identify_data), 3, 3);
597
        uint32_t piotime = 0x11f3;
620
        uint32_t piotime = 0x11f3;
598
        uint32_t mdmatime = 0x1c175;
621
        uint32_t mdmatime = 0x1c175;
599
        uint32_t udmatime = 0x5071152;
622
        uint32_t udmatime = 0x5071152;
600
        uint32_t param = 0;
623
        uint32_t param = 0;
601
        ata_dma_flags = 0;
624
        ata_dma_flags = 0;
Line 647... Line 670...
647
                ata_dma_flags = BIT(2) | BIT(3) | BIT(9) | BIT(10);
670
                ata_dma_flags = BIT(2) | BIT(3) | BIT(9) | BIT(10);
648
                param |= 0x40;
671
                param |= 0x40;
649
            }
672
            }
650
        }
673
        }
651
        ata_dma = param ? true : false;
674
        ata_dma = param ? true : false;
652
        PASS_RC(ata_set_feature(0xef, param), 2, 1);
675
        PASS_RC(ata_set_feature(0x03, param), 3, 4);
653
        if (ata_identify_data[82] & BIT(5)) PASS_RC(ata_set_feature(0x02, 0), 2, 2);
-
 
654
        if (ata_identify_data[82] & BIT(6)) PASS_RC(ata_set_feature(0x55, 0), 2, 3);
-
 
655
        ATA_PIO_TIME = piotime;
676
        ATA_PIO_TIME = piotime;
656
        ATA_MDMA_TIME = mdmatime;
677
        ATA_MDMA_TIME = mdmatime;
657
        ATA_UDMA_TIME = udmatime;
678
        ATA_UDMA_TIME = udmatime;
658
    }
679
    }
-
 
680
    if (ata_identify_data[82] & BIT(5))
-
 
681
        PASS_RC(ata_set_feature(ata_bbt ? 0x82 : 0x02, 0), 3, 5);
-
 
682
    if (ata_identify_data[82] & BIT(6)) PASS_RC(ata_set_feature(0xaa, 0), 3, 6);
659
    if (ata_lba48)
683
    if (ata_lba48)
660
        ata_total_sectors = ata_identify_data[100]
684
        ata_total_sectors = ata_identify_data[100]
661
                            | (((uint64_t)ata_identify_data[101]) << 16)
685
                            | (((uint64_t)ata_identify_data[101]) << 16)
662
                            | (((uint64_t)ata_identify_data[102]) << 32)
686
                            | (((uint64_t)ata_identify_data[102]) << 32)
663
                            | (((uint64_t)ata_identify_data[103]) << 48);
687
                            | (((uint64_t)ata_identify_data[103]) << 48);
Line 676... Line 700...
676
    {
700
    {
677
        memset(ceata_taskfile, 0, 16);
701
        memset(ceata_taskfile, 0, 16);
678
        ceata_taskfile[0xf] = 0xe0;
702
        ceata_taskfile[0xf] = 0xe0;
679
        ceata_wait_idle();
703
        ceata_wait_idle();
680
        ceata_write_multiple_register(0, ceata_taskfile, 16);
704
        ceata_write_multiple_register(0, ceata_taskfile, 16);
-
 
705
        ceata_wait_idle();
681
        sleep(1000000);
706
        sleep(100000);
682
        clockgate_enable(9, false);
707
        clockgate_enable(9, false);
683
    }
708
    }
684
    else
709
    else
685
    {
710
    {
686
        ata_wait_for_rdy(1000000);
711
        ata_wait_for_rdy(1000000);
Line 690... Line 715...
690
        sleep(30000);
715
        sleep(30000);
691
        ATA_CONTROL = 0;
716
        ATA_CONTROL = 0;
692
        while (!(ATA_CONTROL & BIT(1))) yield();
717
        while (!(ATA_CONTROL & BIT(1))) yield();
693
        clockgate_enable(5, false);
718
        clockgate_enable(5, false);
694
    }
719
    }
-
 
720
    PCON(7) = 0;
-
 
721
    PCON(8) = 0;
-
 
722
    PCON(9) = 0;
-
 
723
    PCON(10) &= ~0xffff;
-
 
724
    PCON(11) &= ~0xf;
695
    i2c_sendbyte(0, 0xe6, 0x1b, 0);
725
    i2c_sendbyte(0, 0xe6, 0x1b, 0);
696
}
726
}
697
 
727
 
698
int ata_rw_chunk(uint64_t sector, uint32_t cnt, void* buffer, bool write)
728
int ata_rw_chunk(uint64_t sector, uint32_t cnt, void* buffer, bool write)
699
{
729
{
Line 861... Line 891...
861
        {
891
        {
862
            uint64_t phys;
892
            uint64_t phys;
863
            uint32_t cnt;
893
            uint32_t cnt;
864
            PASS_RC(ata_bbt_translate(sector, count, &phys, &cnt), 0, 0);
894
            PASS_RC(ata_bbt_translate(sector, count, &phys, &cnt), 0, 0);
865
            uint32_t offset = phys - sector;
895
            uint32_t offset = phys - sector;
866
            if (offset != ata_last_offset && phys - ata_last_phys < 64) ata_soft_reset();
896
            if (offset != ata_last_offset && phys - ata_last_phys < 64) ata_reset();
867
            ata_last_offset = offset;
897
            ata_last_offset = offset;
868
            ata_last_phys = phys + cnt;
898
            ata_last_phys = phys + cnt;
869
            PASS_RC(ata_rw_sectors_internal(phys, cnt, buffer, write), 0, 0);
899
            PASS_RC(ata_rw_sectors_internal(phys, cnt, buffer, write), 0, 0);
870
            buffer += cnt * SECTOR_SIZE;
900
            buffer += cnt * SECTOR_SIZE;
871
            sector += cnt;
901
            sector += cnt;
Line 887... Line 917...
887
    while (count)
917
    while (count)
888
    {
918
    {
889
        uint32_t cnt = MIN(ata_lba48 ? 8192 : 32, count);
919
        uint32_t cnt = MIN(ata_lba48 ? 8192 : 32, count);
890
        int rc = -1;
920
        int rc = -1;
891
        rc = ata_rw_chunk(sector, cnt, buffer, write);
921
        rc = ata_rw_chunk(sector, cnt, buffer, write);
892
        if (rc && ata_error_srst) ata_soft_reset();
922
        if (rc && ata_error_srst) ata_reset();
893
        if (rc && ata_retries)
923
        if (rc && ata_retries)
894
        {
924
        {
895
            void* buf = buffer;
925
            void* buf = buffer;
896
            uint64_t sect;
926
            uint64_t sect;
897
            for (sect = sector; sect < sector + cnt; sect++)
927
            for (sect = sector; sect < sector + cnt; sect++)
Line 899... Line 929...
899
                rc = -1;
929
                rc = -1;
900
                int tries = ata_retries;
930
                int tries = ata_retries;
901
                while (tries-- && rc)
931
                while (tries-- && rc)
902
                {
932
                {
903
                    rc = ata_rw_chunk(sect, 1, buf, write);
933
                    rc = ata_rw_chunk(sect, 1, buf, write);
904
                    if (rc && ata_error_srst) ata_soft_reset();
934
                    if (rc && ata_error_srst) ata_reset();
905
                }
935
                }
906
                if (rc) break;
936
                if (rc) break;
907
                buf += SECTOR_SIZE;
937
                buf += SECTOR_SIZE;
908
            }
938
            }
909
        }
939
        }
Line 939... Line 969...
939
    else
969
    else
940
    {
970
    {
941
        ata_write_cbr(&ATA_PIO_DAD, BIT(1) | BIT(2));
971
        ata_write_cbr(&ATA_PIO_DAD, BIT(1) | BIT(2));
942
        sleep(10);
972
        sleep(10);
943
        ata_write_cbr(&ATA_PIO_DAD, 0);
973
        ata_write_cbr(&ATA_PIO_DAD, 0);
944
        rc = ata_wait_for_rdy(20000000);
974
        rc = ata_wait_for_rdy(3000000);
945
    }
975
    }
-
 
976
    ata_set_active();
-
 
977
    mutex_unlock(&ata_mutex);
-
 
978
    PASS_RC(rc, 1, 1);
-
 
979
    return 0;
-
 
980
}
-
 
981
 
-
 
982
int ata_hard_reset()
-
 
983
{
-
 
984
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
-
 
985
    PASS_RC(ata_power_up(), 0, 0);
-
 
986
    ata_set_active();
-
 
987
    mutex_unlock(&ata_mutex);
-
 
988
    return 0;
-
 
989
}
-
 
990
 
-
 
991
int ata_reset()
-
 
992
{
-
 
993
    int rc;
-
 
994
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
-
 
995
    if (!ata_powered) PASS_RC(ata_power_up(), 2, 0);
-
 
996
    ata_set_active();
-
 
997
    rc = ata_soft_reset();
946
    if (IS_ERR(rc))
998
    if (IS_ERR(rc))
947
    {
999
    {
-
 
1000
        rc = ata_hard_reset();
-
 
1001
        if (IS_ERR(rc))
-
 
1002
        {
-
 
1003
            rc = ERR_RC((rc << 2) | 1);
948
        ata_power_down();
1004
            ata_power_down();
949
        sleep(3000000);
1005
            sleep(3000000);
950
        ata_power_up();
1006
            int rc2 = ata_power_up();
-
 
1007
            if (IS_ERR(rc2)) rc = ERR_RC((rc << 2) | 2);
-
 
1008
        }
-
 
1009
        else rc = 1;
951
    }
1010
    }
952
    ata_set_active();
1011
    ata_set_active();
953
    mutex_unlock(&ata_mutex);
1012
    mutex_unlock(&ata_mutex);
954
    PASS_RC(rc, 1, 1);
1013
    return rc;
955
}
1014
}
956
 
1015
 
957
int ata_read_sectors(IF_MD2(int drive,) unsigned long start, int incount,
1016
int ata_read_sectors(IF_MD2(int drive,) unsigned long start, int incount,
958
                     void* inbuf)
1017
                     void* inbuf)
959
{
1018
{
Line 1030... Line 1089...
1030
 
1089
 
1031
int ata_bbt_reload()
1090
int ata_bbt_reload()
1032
{
1091
{
1033
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
1092
    mutex_lock(&ata_mutex, TIMEOUT_BLOCK);
1034
    ata_bbt_disable();
1093
    ata_bbt_disable();
1035
    PASS_RC(ata_power_up(), 0, 0);
1094
    PASS_RC(ata_power_up(), 1, 0);
1036
    uint32_t* buf = (uint32_t*)memalign(0x10, 0x1000);
1095
    uint32_t* buf = (uint32_t*)memalign(0x10, 0x1000);
1037
    if (buf)
1096
    if (buf)
1038
    {
1097
    {
1039
        if (IS_ERR(ata_bbt_read_sectors(0, 1, buf)))
1098
        if (IS_ERR(ata_bbt_read_sectors(0, 1, buf)))
1040
            ata_virtual_sectors = ata_total_sectors;
1099
            ata_virtual_sectors = ata_total_sectors;
1041
        else if (!memcmp(buf, "emBIbbth", 8))
1100
        else if (!memcmp(buf, "emBIbbth", 8))
1042
        {
1101
        {
-
 
1102
            if (ata_identify_data[82] & BIT(5)) PASS_RC(ata_set_feature(0x02, 0), 1, 1);
1043
            ata_virtual_sectors = (((uint64_t)buf[0x1fd]) << 32) | buf[0x1fc];
1103
            ata_virtual_sectors = (((uint64_t)buf[0x1fd]) << 32) | buf[0x1fc];
1044
            uint32_t count = buf[0x1ff];
1104
            uint32_t count = buf[0x1ff];
1045
            ata_bbt = (typeof(ata_bbt))memalign(0x10, 0x1000 * count);
1105
            ata_bbt = (typeof(ata_bbt))memalign(0x10, 0x1000 * count);
1046
            if (!ata_bbt)
1106
            if (!ata_bbt)
1047
            {
1107
            {
Line 1072... Line 1132...
1072
        else ata_virtual_sectors = ata_total_sectors;
1132
        else ata_virtual_sectors = ata_total_sectors;
1073
        free(buf);
1133
        free(buf);
1074
    }
1134
    }
1075
    else ata_virtual_sectors = ata_total_sectors;
1135
    else ata_virtual_sectors = ata_total_sectors;
1076
    mutex_unlock(&ata_mutex);
1136
    mutex_unlock(&ata_mutex);
-
 
1137
    return 0;
1077
}
1138
}
1078
#endif
1139
#endif
1079
 
1140
 
1080
int ata_init(void)
1141
int ata_init(void)
1081
{
1142
{
1082
    mutex_init(&ata_mutex);
1143
    mutex_init(&ata_mutex);
1083
    wakeup_init(&ata_wakeup);
1144
    wakeup_init(&ata_wakeup);
1084
    wakeup_init(&mmc_wakeup);
1145
    wakeup_init(&mmc_wakeup);
1085
    wakeup_init(&mmc_comp_wakeup);
1146
    wakeup_init(&mmc_comp_wakeup);
1086
    ceata = PDAT(11) & BIT(1);
1147
    ceata = PDAT(11) & BIT(1);
1087
    if (ceata)
-
 
1088
    {
-
 
1089
        ata_lba48 = true;
-
 
1090
        ata_dma = true;
-
 
1091
        PCON(8) = 0x33333333;
-
 
1092
        PCON(9) = (PCON(9) & ~0xff) | 0x33;
-
 
1093
        PCON(11) |= 0xf;
-
 
1094
        *((uint32_t volatile*)0x38a00000) = 0;
-
 
1095
        *((uint32_t volatile*)0x38700000) = 0;
-
 
1096
    }
-
 
1097
    else
-
 
1098
    {
-
 
1099
        PCON(7) = 0x44444444;
-
 
1100
        PCON(8) = 0x44444444;
-
 
1101
        PCON(9) = 0x44444444;
-
 
1102
        PCON(10) = (PCON(10) & ~0xffff) | 0x4444;
-
 
1103
    }
-
 
1104
    ata_powered = false;
1148
    ata_powered = false;
1105
    ata_total_sectors = 0;
1149
    ata_total_sectors = 0;
1106
#ifdef ATA_HAVE_BBT
1150
#ifdef ATA_HAVE_BBT
1107
    PASS_RC(ata_bbt_reload(), 0, 0);
1151
    PASS_RC(ata_bbt_reload(), 0, 0);
1108
#endif
1152
#endif