989690a33e0d3a45e6c5549d8adc126af707799a
[oweals/u-boot.git] / drivers / ata / ahci_sunxi.c
1 #include <common.h>
2 #include <ahci.h>
3 #include <dm.h>
4 #include <log.h>
5 #include <scsi.h>
6 #include <errno.h>
7 #include <asm/io.h>
8 #include <asm/gpio.h>
9
10 #define AHCI_PHYCS0R 0x00c0
11 #define AHCI_PHYCS1R 0x00c4
12 #define AHCI_PHYCS2R 0x00c8
13 #define AHCI_RWCR    0x00fc
14
15 /* This magic PHY initialisation was taken from the Allwinner releases
16  * and Linux driver, but is completely undocumented.
17  */
18 static int sunxi_ahci_phy_init(u8 *reg_base)
19 {
20         u32 reg_val;
21         int timeout;
22
23         writel(0, reg_base + AHCI_RWCR);
24         mdelay(5);
25
26         setbits_le32(reg_base + AHCI_PHYCS1R, 0x1 << 19);
27         clrsetbits_le32(reg_base + AHCI_PHYCS0R,
28                         (0x7 << 24),
29                         (0x5 << 24) | (0x1 << 23) | (0x1 << 18));
30         clrsetbits_le32(reg_base + AHCI_PHYCS1R,
31                         (0x3 << 16) | (0x1f << 8) | (0x3 << 6),
32                         (0x2 << 16) | (0x6 << 8) | (0x2 << 6));
33         setbits_le32(reg_base + AHCI_PHYCS1R, (0x1 << 28) | (0x1 << 15));
34         clrbits_le32(reg_base + AHCI_PHYCS1R, (0x1 << 19));
35         clrsetbits_le32(reg_base + AHCI_PHYCS0R, (0x7 << 20), (0x3 << 20));
36         clrsetbits_le32(reg_base + AHCI_PHYCS2R, (0x1f << 5), (0x19 << 5));
37         mdelay(5);
38
39         setbits_le32(reg_base + AHCI_PHYCS0R, (0x1 << 19));
40
41         timeout = 250; /* Power up takes approx 50 us */
42         for (;;) {
43                 reg_val = readl(reg_base + AHCI_PHYCS0R) & (0x7 << 28);
44                 if (reg_val == (0x2 << 28))
45                         break;
46                 if (--timeout == 0) {
47                         printf("AHCI PHY power up failed.\n");
48                         return -EIO;
49                 }
50                 udelay(1);
51         };
52
53         setbits_le32(reg_base + AHCI_PHYCS2R, (0x1 << 24));
54
55         timeout = 100; /* Calibration takes approx 10 us */
56         for (;;) {
57                 reg_val = readl(reg_base + AHCI_PHYCS2R) & (0x1 << 24);
58                 if (reg_val == 0x0)
59                         break;
60                 if (--timeout == 0) {
61                         printf("AHCI PHY calibration failed.\n");
62                         return -EIO;
63                 }
64                 udelay(1);
65         }
66
67         mdelay(15);
68
69         writel(0x7, reg_base + AHCI_RWCR);
70
71         return 0;
72 }
73
74 static int sunxi_sata_probe(struct udevice *dev)
75 {
76         ulong base;
77         u8 *reg;
78         int ret;
79
80         base = dev_read_addr(dev);
81         if (base == FDT_ADDR_T_NONE) {
82                 debug("%s: Failed to find address (err=%d\n)", __func__, ret);
83                 return -EINVAL;
84         }
85         reg = (u8 *)base;
86         ret = sunxi_ahci_phy_init(reg);
87         if (ret) {
88                 debug("%s: Failed to init phy (err=%d\n)", __func__, ret);
89                 return ret;
90         }
91         ret = ahci_probe_scsi(dev, base);
92         if (ret) {
93                 debug("%s: Failed to probe (err=%d\n)", __func__, ret);
94                 return ret;
95         }
96
97         return 0;
98 }
99
100 static int sunxi_sata_bind(struct udevice *dev)
101 {
102         struct udevice *scsi_dev;
103         int ret;
104
105         ret = ahci_bind_scsi(dev, &scsi_dev);
106         if (ret) {
107                 debug("%s: Failed to bind (err=%d\n)", __func__, ret);
108                 return ret;
109         }
110
111         return 0;
112 }
113
114 static const struct udevice_id sunxi_ahci_ids[] = {
115         { .compatible = "allwinner,sun4i-a10-ahci" },
116         { .compatible = "allwinner,sun8i-r40-ahci" },
117         { }
118 };
119
120 U_BOOT_DRIVER(ahci_sunxi_drv) = {
121         .name           = "ahci_sunxi",
122         .id             = UCLASS_AHCI,
123         .of_match       = sunxi_ahci_ids,
124         .bind           = sunxi_sata_bind,
125         .probe          = sunxi_sata_probe,
126 };