1 // SPDX-License-Identifier: GPL-2.0-only
3 * MediaTek AHCI SATA driver
5 * Copyright (c) 2017 MediaTek Inc.
6 * Author: Ryder Lee <ryder.lee@mediatek.com>
9 #include <linux/ahci_platform.h>
10 #include <linux/kernel.h>
11 #include <linux/libata.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/reset.h>
21 #define DRV_NAME "ahci-mtk"
24 #define SYS_CFG_SATA_MSK GENMASK(31, 30)
25 #define SYS_CFG_SATA_EN BIT(31)
27 struct mtk_ahci_plat {
29 struct reset_control *axi_rst;
30 struct reset_control *sw_rst;
31 struct reset_control *reg_rst;
34 static const struct ata_port_info ahci_port_info = {
35 .flags = AHCI_FLAG_COMMON,
37 .udma_mask = ATA_UDMA6,
38 .port_ops = &ahci_platform_ops,
41 static const struct scsi_host_template ahci_platform_sht = {
45 static int mtk_ahci_platform_resets(struct ahci_host_priv *hpriv,
48 struct mtk_ahci_plat *plat = hpriv->plat_data;
51 /* reset AXI bus and PHY part */
52 plat->axi_rst = devm_reset_control_get_optional_exclusive(dev, "axi");
53 if (PTR_ERR(plat->axi_rst) == -EPROBE_DEFER)
54 return PTR_ERR(plat->axi_rst);
56 plat->sw_rst = devm_reset_control_get_optional_exclusive(dev, "sw");
57 if (PTR_ERR(plat->sw_rst) == -EPROBE_DEFER)
58 return PTR_ERR(plat->sw_rst);
60 plat->reg_rst = devm_reset_control_get_optional_exclusive(dev, "reg");
61 if (PTR_ERR(plat->reg_rst) == -EPROBE_DEFER)
62 return PTR_ERR(plat->reg_rst);
64 err = reset_control_assert(plat->axi_rst);
66 dev_err(dev, "failed to assert AXI bus\n");
70 err = reset_control_assert(plat->sw_rst);
72 dev_err(dev, "failed to assert PHY digital part\n");
76 err = reset_control_assert(plat->reg_rst);
78 dev_err(dev, "failed to assert PHY register part\n");
82 err = reset_control_deassert(plat->reg_rst);
84 dev_err(dev, "failed to deassert PHY register part\n");
88 err = reset_control_deassert(plat->sw_rst);
90 dev_err(dev, "failed to deassert PHY digital part\n");
94 err = reset_control_deassert(plat->axi_rst);
96 dev_err(dev, "failed to deassert AXI bus\n");
103 static int mtk_ahci_parse_property(struct ahci_host_priv *hpriv,
106 struct mtk_ahci_plat *plat = hpriv->plat_data;
107 struct device_node *np = dev->of_node;
109 /* enable SATA function if needed */
110 if (of_property_present(np, "mediatek,phy-mode")) {
111 plat->mode = syscon_regmap_lookup_by_phandle(
112 np, "mediatek,phy-mode");
113 if (IS_ERR(plat->mode)) {
114 dev_err(dev, "missing phy-mode phandle\n");
115 return PTR_ERR(plat->mode);
118 regmap_update_bits(plat->mode, SYS_CFG, SYS_CFG_SATA_MSK,
125 static int mtk_ahci_probe(struct platform_device *pdev)
127 struct device *dev = &pdev->dev;
128 struct mtk_ahci_plat *plat;
129 struct ahci_host_priv *hpriv;
132 plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
136 hpriv = ahci_platform_get_resources(pdev, 0);
138 return PTR_ERR(hpriv);
140 hpriv->plat_data = plat;
142 err = mtk_ahci_parse_property(hpriv, dev);
146 err = mtk_ahci_platform_resets(hpriv, dev);
150 err = ahci_platform_enable_resources(hpriv);
154 err = ahci_platform_init_host(pdev, hpriv, &ahci_port_info,
157 goto disable_resources;
162 ahci_platform_disable_resources(hpriv);
166 static SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_platform_suspend,
167 ahci_platform_resume);
169 static const struct of_device_id ahci_of_match[] = {
170 { .compatible = "mediatek,mtk-ahci", },
173 MODULE_DEVICE_TABLE(of, ahci_of_match);
175 static struct platform_driver mtk_ahci_driver = {
176 .probe = mtk_ahci_probe,
177 .remove_new = ata_platform_remove_one,
180 .of_match_table = ahci_of_match,
184 module_platform_driver(mtk_ahci_driver);
186 MODULE_DESCRIPTION("MediaTek SATA AHCI Driver");
187 MODULE_LICENSE("GPL v2");