blob: 6cf5cee055e7c20668a8310ebaeac9ac0183b2a4 [file] [log] [blame]
Ian Campbella2ebf922014-07-18 20:38:41 +01001#include <ahci.h>
Simon Glass331c8ef2017-07-04 13:31:31 -06002#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06003#include <log.h>
Ian Campbella2ebf922014-07-18 20:38:41 +01004#include <scsi.h>
5#include <errno.h>
6#include <asm/io.h>
7#include <asm/gpio.h>
Simon Glassdbd79542020-05-10 11:40:11 -06008#include <linux/delay.h>
Andre Przywara8ee04092022-07-15 16:52:14 +01009#include <power/regulator.h>
Ian Campbella2ebf922014-07-18 20:38:41 +010010
11#define AHCI_PHYCS0R 0x00c0
12#define AHCI_PHYCS1R 0x00c4
13#define AHCI_PHYCS2R 0x00c8
14#define AHCI_RWCR 0x00fc
15
16/* This magic PHY initialisation was taken from the Allwinner releases
17 * and Linux driver, but is completely undocumented.
18 */
Simon Glass331c8ef2017-07-04 13:31:31 -060019static int sunxi_ahci_phy_init(u8 *reg_base)
Ian Campbella2ebf922014-07-18 20:38:41 +010020{
Ian Campbella2ebf922014-07-18 20:38:41 +010021 u32 reg_val;
22 int timeout;
23
24 writel(0, reg_base + AHCI_RWCR);
25 mdelay(5);
26
27 setbits_le32(reg_base + AHCI_PHYCS1R, 0x1 << 19);
28 clrsetbits_le32(reg_base + AHCI_PHYCS0R,
29 (0x7 << 24),
30 (0x5 << 24) | (0x1 << 23) | (0x1 << 18));
31 clrsetbits_le32(reg_base + AHCI_PHYCS1R,
32 (0x3 << 16) | (0x1f << 8) | (0x3 << 6),
33 (0x2 << 16) | (0x6 << 8) | (0x2 << 6));
34 setbits_le32(reg_base + AHCI_PHYCS1R, (0x1 << 28) | (0x1 << 15));
35 clrbits_le32(reg_base + AHCI_PHYCS1R, (0x1 << 19));
36 clrsetbits_le32(reg_base + AHCI_PHYCS0R, (0x7 << 20), (0x3 << 20));
37 clrsetbits_le32(reg_base + AHCI_PHYCS2R, (0x1f << 5), (0x19 << 5));
38 mdelay(5);
39
40 setbits_le32(reg_base + AHCI_PHYCS0R, (0x1 << 19));
41
42 timeout = 250; /* Power up takes approx 50 us */
43 for (;;) {
44 reg_val = readl(reg_base + AHCI_PHYCS0R) & (0x7 << 28);
45 if (reg_val == (0x2 << 28))
46 break;
47 if (--timeout == 0) {
48 printf("AHCI PHY power up failed.\n");
49 return -EIO;
50 }
51 udelay(1);
52 };
53
54 setbits_le32(reg_base + AHCI_PHYCS2R, (0x1 << 24));
55
56 timeout = 100; /* Calibration takes approx 10 us */
57 for (;;) {
58 reg_val = readl(reg_base + AHCI_PHYCS2R) & (0x1 << 24);
59 if (reg_val == 0x0)
60 break;
61 if (--timeout == 0) {
62 printf("AHCI PHY calibration failed.\n");
63 return -EIO;
64 }
65 udelay(1);
66 }
67
68 mdelay(15);
69
70 writel(0x7, reg_base + AHCI_RWCR);
71
72 return 0;
73}
74
Simon Glass331c8ef2017-07-04 13:31:31 -060075static int sunxi_sata_probe(struct udevice *dev)
76{
Andre Przywara8ee04092022-07-15 16:52:14 +010077 struct udevice *reg_dev;
Simon Glass331c8ef2017-07-04 13:31:31 -060078 ulong base;
79 u8 *reg;
80 int ret;
81
82 base = dev_read_addr(dev);
83 if (base == FDT_ADDR_T_NONE) {
Dario Binacchi6ddb8252021-01-24 19:13:10 +010084 debug("%s: Failed to find address\n", __func__);
Simon Glass331c8ef2017-07-04 13:31:31 -060085 return -EINVAL;
86 }
87 reg = (u8 *)base;
88 ret = sunxi_ahci_phy_init(reg);
89 if (ret) {
Dario Binacchi6ddb8252021-01-24 19:13:10 +010090 debug("%s: Failed to init phy (err=%d)\n", __func__, ret);
Simon Glass331c8ef2017-07-04 13:31:31 -060091 return ret;
92 }
Andre Przywara8ee04092022-07-15 16:52:14 +010093
94 ret = device_get_supply_regulator(dev, "target-supply", &reg_dev);
95 if (ret == 0) {
96 regulator_set_enable(reg_dev, true);
97 mdelay(500);
98 }
99
Simon Glass331c8ef2017-07-04 13:31:31 -0600100 ret = ahci_probe_scsi(dev, base);
101 if (ret) {
Dario Binacchi6ddb8252021-01-24 19:13:10 +0100102 debug("%s: Failed to probe (err=%d)\n", __func__, ret);
Simon Glass331c8ef2017-07-04 13:31:31 -0600103 return ret;
104 }
105
106 return 0;
107}
108
109static int sunxi_sata_bind(struct udevice *dev)
110{
111 struct udevice *scsi_dev;
112 int ret;
113
114 ret = ahci_bind_scsi(dev, &scsi_dev);
115 if (ret) {
Dario Binacchi6ddb8252021-01-24 19:13:10 +0100116 debug("%s: Failed to bind (err=%d)\n", __func__, ret);
Simon Glass331c8ef2017-07-04 13:31:31 -0600117 return ret;
118 }
119
120 return 0;
121}
122
123static const struct udevice_id sunxi_ahci_ids[] = {
124 { .compatible = "allwinner,sun4i-a10-ahci" },
Jagan Tekifacfc1b2019-04-12 16:47:56 +0530125 { .compatible = "allwinner,sun8i-r40-ahci" },
Simon Glass331c8ef2017-07-04 13:31:31 -0600126 { }
127};
128
129U_BOOT_DRIVER(ahci_sunxi_drv) = {
130 .name = "ahci_sunxi",
131 .id = UCLASS_AHCI,
132 .of_match = sunxi_ahci_ids,
133 .bind = sunxi_sata_bind,
134 .probe = sunxi_sata_probe,
135};