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>
14 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/reset.h>
20 #define DRV_NAME "ahci-mtk"
23 #define SYS_CFG_SATA_MSK GENMASK(31, 30)
24 #define SYS_CFG_SATA_EN BIT(31)
26 struct mtk_ahci_plat
{
28 struct reset_control
*axi_rst
;
29 struct reset_control
*sw_rst
;
30 struct reset_control
*reg_rst
;
33 static const struct ata_port_info ahci_port_info
= {
34 .flags
= AHCI_FLAG_COMMON
,
36 .udma_mask
= ATA_UDMA6
,
37 .port_ops
= &ahci_platform_ops
,
40 static struct scsi_host_template ahci_platform_sht
= {
44 static int mtk_ahci_platform_resets(struct ahci_host_priv
*hpriv
,
47 struct mtk_ahci_plat
*plat
= hpriv
->plat_data
;
50 /* reset AXI bus and PHY part */
51 plat
->axi_rst
= devm_reset_control_get_optional_exclusive(dev
, "axi");
52 if (PTR_ERR(plat
->axi_rst
) == -EPROBE_DEFER
)
53 return PTR_ERR(plat
->axi_rst
);
55 plat
->sw_rst
= devm_reset_control_get_optional_exclusive(dev
, "sw");
56 if (PTR_ERR(plat
->sw_rst
) == -EPROBE_DEFER
)
57 return PTR_ERR(plat
->sw_rst
);
59 plat
->reg_rst
= devm_reset_control_get_optional_exclusive(dev
, "reg");
60 if (PTR_ERR(plat
->reg_rst
) == -EPROBE_DEFER
)
61 return PTR_ERR(plat
->reg_rst
);
63 err
= reset_control_assert(plat
->axi_rst
);
65 dev_err(dev
, "failed to assert AXI bus\n");
69 err
= reset_control_assert(plat
->sw_rst
);
71 dev_err(dev
, "failed to assert PHY digital part\n");
75 err
= reset_control_assert(plat
->reg_rst
);
77 dev_err(dev
, "failed to assert PHY register part\n");
81 err
= reset_control_deassert(plat
->reg_rst
);
83 dev_err(dev
, "failed to deassert PHY register part\n");
87 err
= reset_control_deassert(plat
->sw_rst
);
89 dev_err(dev
, "failed to deassert PHY digital part\n");
93 err
= reset_control_deassert(plat
->axi_rst
);
95 dev_err(dev
, "failed to deassert AXI bus\n");
102 static int mtk_ahci_parse_property(struct ahci_host_priv
*hpriv
,
105 struct mtk_ahci_plat
*plat
= hpriv
->plat_data
;
106 struct device_node
*np
= dev
->of_node
;
108 /* enable SATA function if needed */
109 if (of_find_property(np
, "mediatek,phy-mode", NULL
)) {
110 plat
->mode
= syscon_regmap_lookup_by_phandle(
111 np
, "mediatek,phy-mode");
112 if (IS_ERR(plat
->mode
)) {
113 dev_err(dev
, "missing phy-mode phandle\n");
114 return PTR_ERR(plat
->mode
);
117 regmap_update_bits(plat
->mode
, SYS_CFG
, SYS_CFG_SATA_MSK
,
121 of_property_read_u32(np
, "ports-implemented", &hpriv
->force_port_map
);
126 static int mtk_ahci_probe(struct platform_device
*pdev
)
128 struct device
*dev
= &pdev
->dev
;
129 struct mtk_ahci_plat
*plat
;
130 struct ahci_host_priv
*hpriv
;
133 plat
= devm_kzalloc(dev
, sizeof(*plat
), GFP_KERNEL
);
137 hpriv
= ahci_platform_get_resources(pdev
, 0);
139 return PTR_ERR(hpriv
);
141 hpriv
->plat_data
= plat
;
143 err
= mtk_ahci_parse_property(hpriv
, dev
);
147 err
= mtk_ahci_platform_resets(hpriv
, dev
);
151 err
= ahci_platform_enable_resources(hpriv
);
155 err
= ahci_platform_init_host(pdev
, hpriv
, &ahci_port_info
,
158 goto disable_resources
;
163 ahci_platform_disable_resources(hpriv
);
167 static SIMPLE_DEV_PM_OPS(ahci_pm_ops
, ahci_platform_suspend
,
168 ahci_platform_resume
);
170 static const struct of_device_id ahci_of_match
[] = {
171 { .compatible
= "mediatek,mtk-ahci", },
174 MODULE_DEVICE_TABLE(of
, ahci_of_match
);
176 static struct platform_driver mtk_ahci_driver
= {
177 .probe
= mtk_ahci_probe
,
178 .remove
= ata_platform_remove_one
,
181 .of_match_table
= ahci_of_match
,
185 module_platform_driver(mtk_ahci_driver
);
187 MODULE_DESCRIPTION("MediaTek SATA AHCI Driver");
188 MODULE_LICENSE("GPL v2");