Merge tag 'u-boot-imx-20200825' of https://gitlab.denx.de/u-boot/custodians/u-boot-imx
[platform/kernel/u-boot.git] / drivers / ata / mtk_ahci.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * MTK SATA platform driver
4  *
5  * Copyright (C) 2020 MediaTek Inc.
6  *
7  * Author: Ryder Lee <ryder.lee@mediatek.com>
8  * Author: Frank Wunderlich <frank-w@public-files.de>
9  */
10
11 #include <common.h>
12 #include <ahci.h>
13 #include <asm/io.h>
14 #include <dm.h>
15 #include <dm/of_access.h>
16 #include <generic-phy.h>
17 #include <linux/err.h>
18 #include <regmap.h>
19 #include <reset.h>
20 #include <sata.h>
21 #include <scsi.h>
22 #include <syscon.h>
23
24 #define SYS_CFG                 0x14
25 #define SYS_CFG_SATA_MSK        GENMASK(31, 30)
26 #define SYS_CFG_SATA_EN         BIT(31)
27
28 struct mtk_ahci_priv {
29         void *base;
30
31         struct ahci_uc_priv ahci_priv;
32         struct regmap *mode;
33         struct reset_ctl_bulk rst_bulk;
34 };
35
36 static int mtk_ahci_bind(struct udevice *dev)
37 {
38         struct udevice *scsi_dev;
39
40         return ahci_bind_scsi(dev, &scsi_dev);
41 }
42
43 static int mtk_ahci_ofdata_to_platdata(struct udevice *dev)
44 {
45         struct mtk_ahci_priv *priv = dev_get_priv(dev);
46
47         priv->base = devfdt_remap_addr_index(dev, 0);
48
49         return 0;
50 }
51
52 static int mtk_ahci_parse_property(struct ahci_uc_priv *hpriv,
53                                    struct udevice *dev)
54 {
55         struct mtk_ahci_priv *plat = dev_get_priv(dev);
56         const void *fdt = gd->fdt_blob;
57
58         /* enable SATA function if needed */
59         if (fdt_get_property(fdt, dev_of_offset(dev),
60                              "mediatek,phy-mode", NULL)) {
61                 plat->mode = syscon_regmap_lookup_by_phandle(dev,
62                                                 "mediatek,phy-mode");
63                 if (IS_ERR(plat->mode)) {
64                         dev_err(dev, "missing phy-mode phandle\n");
65                         return PTR_ERR(plat->mode);
66                 }
67                 regmap_update_bits(plat->mode, SYS_CFG,
68                                    SYS_CFG_SATA_MSK, SYS_CFG_SATA_EN);
69         }
70
71         ofnode_read_u32(dev->node, "ports-implemented", &hpriv->port_map);
72         return 0;
73 }
74
75 static int mtk_ahci_probe(struct udevice *dev)
76 {
77         struct mtk_ahci_priv *priv = dev_get_priv(dev);
78         int ret;
79         struct phy phy;
80
81         ret = mtk_ahci_parse_property(&priv->ahci_priv, dev);
82         if (ret)
83                 return ret;
84
85         ret = reset_get_bulk(dev, &priv->rst_bulk);
86         if (!ret) {
87                 reset_assert_bulk(&priv->rst_bulk);
88                 reset_deassert_bulk(&priv->rst_bulk);
89         } else {
90                 dev_err(dev, "Failed to get reset: %d\n", ret);
91         }
92
93         ret = generic_phy_get_by_name(dev, "sata-phy", &phy);
94         if (ret) {
95                 pr_err("can't get the phy from DT\n");
96                 return ret;
97         }
98
99         ret = generic_phy_init(&phy);
100         if (ret) {
101                 pr_err("unable to initialize the sata phy\n");
102                 return ret;
103         }
104
105         ret = generic_phy_power_on(&phy);
106         if (ret) {
107                 pr_err("unable to power on the sata phy\n");
108                 return ret;
109         }
110
111         return ahci_probe_scsi(dev, (ulong)priv->base);
112 }
113
114 static const struct udevice_id mtk_ahci_ids[] = {
115         { .compatible = "mediatek,mtk-ahci" },
116         { }
117 };
118
119 U_BOOT_DRIVER(mtk_ahci) = {
120         .name   = "mtk_ahci",
121         .id     = UCLASS_AHCI,
122         .of_match = mtk_ahci_ids,
123         .bind   = mtk_ahci_bind,
124         .ofdata_to_platdata = mtk_ahci_ofdata_to_platdata,
125         .ops    = &scsi_ops,
126         .probe  = mtk_ahci_probe,
127         .priv_auto_alloc_size = sizeof(struct mtk_ahci_priv),
128 };