1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * MediaTek AHCI SATA driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2017 MediaTek Inc.
6*4882a593Smuzhiyun * Author: Ryder Lee <ryder.lee@mediatek.com>
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/ahci_platform.h>
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/libata.h>
12*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/platform_device.h>
15*4882a593Smuzhiyun #include <linux/pm.h>
16*4882a593Smuzhiyun #include <linux/regmap.h>
17*4882a593Smuzhiyun #include <linux/reset.h>
18*4882a593Smuzhiyun #include "ahci.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define DRV_NAME "ahci-mtk"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #define SYS_CFG 0x14
23*4882a593Smuzhiyun #define SYS_CFG_SATA_MSK GENMASK(31, 30)
24*4882a593Smuzhiyun #define SYS_CFG_SATA_EN BIT(31)
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun struct mtk_ahci_plat {
27*4882a593Smuzhiyun struct regmap *mode;
28*4882a593Smuzhiyun struct reset_control *axi_rst;
29*4882a593Smuzhiyun struct reset_control *sw_rst;
30*4882a593Smuzhiyun struct reset_control *reg_rst;
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun static const struct ata_port_info ahci_port_info = {
34*4882a593Smuzhiyun .flags = AHCI_FLAG_COMMON,
35*4882a593Smuzhiyun .pio_mask = ATA_PIO4,
36*4882a593Smuzhiyun .udma_mask = ATA_UDMA6,
37*4882a593Smuzhiyun .port_ops = &ahci_platform_ops,
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun static struct scsi_host_template ahci_platform_sht = {
41*4882a593Smuzhiyun AHCI_SHT(DRV_NAME),
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun
mtk_ahci_platform_resets(struct ahci_host_priv * hpriv,struct device * dev)44*4882a593Smuzhiyun static int mtk_ahci_platform_resets(struct ahci_host_priv *hpriv,
45*4882a593Smuzhiyun struct device *dev)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun struct mtk_ahci_plat *plat = hpriv->plat_data;
48*4882a593Smuzhiyun int err;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* reset AXI bus and PHY part */
51*4882a593Smuzhiyun plat->axi_rst = devm_reset_control_get_optional_exclusive(dev, "axi");
52*4882a593Smuzhiyun if (PTR_ERR(plat->axi_rst) == -EPROBE_DEFER)
53*4882a593Smuzhiyun return PTR_ERR(plat->axi_rst);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun plat->sw_rst = devm_reset_control_get_optional_exclusive(dev, "sw");
56*4882a593Smuzhiyun if (PTR_ERR(plat->sw_rst) == -EPROBE_DEFER)
57*4882a593Smuzhiyun return PTR_ERR(plat->sw_rst);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun plat->reg_rst = devm_reset_control_get_optional_exclusive(dev, "reg");
60*4882a593Smuzhiyun if (PTR_ERR(plat->reg_rst) == -EPROBE_DEFER)
61*4882a593Smuzhiyun return PTR_ERR(plat->reg_rst);
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun err = reset_control_assert(plat->axi_rst);
64*4882a593Smuzhiyun if (err) {
65*4882a593Smuzhiyun dev_err(dev, "failed to assert AXI bus\n");
66*4882a593Smuzhiyun return err;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun err = reset_control_assert(plat->sw_rst);
70*4882a593Smuzhiyun if (err) {
71*4882a593Smuzhiyun dev_err(dev, "failed to assert PHY digital part\n");
72*4882a593Smuzhiyun return err;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun err = reset_control_assert(plat->reg_rst);
76*4882a593Smuzhiyun if (err) {
77*4882a593Smuzhiyun dev_err(dev, "failed to assert PHY register part\n");
78*4882a593Smuzhiyun return err;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun err = reset_control_deassert(plat->reg_rst);
82*4882a593Smuzhiyun if (err) {
83*4882a593Smuzhiyun dev_err(dev, "failed to deassert PHY register part\n");
84*4882a593Smuzhiyun return err;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun err = reset_control_deassert(plat->sw_rst);
88*4882a593Smuzhiyun if (err) {
89*4882a593Smuzhiyun dev_err(dev, "failed to deassert PHY digital part\n");
90*4882a593Smuzhiyun return err;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun err = reset_control_deassert(plat->axi_rst);
94*4882a593Smuzhiyun if (err) {
95*4882a593Smuzhiyun dev_err(dev, "failed to deassert AXI bus\n");
96*4882a593Smuzhiyun return err;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun return 0;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
mtk_ahci_parse_property(struct ahci_host_priv * hpriv,struct device * dev)102*4882a593Smuzhiyun static int mtk_ahci_parse_property(struct ahci_host_priv *hpriv,
103*4882a593Smuzhiyun struct device *dev)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun struct mtk_ahci_plat *plat = hpriv->plat_data;
106*4882a593Smuzhiyun struct device_node *np = dev->of_node;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /* enable SATA function if needed */
109*4882a593Smuzhiyun if (of_find_property(np, "mediatek,phy-mode", NULL)) {
110*4882a593Smuzhiyun plat->mode = syscon_regmap_lookup_by_phandle(
111*4882a593Smuzhiyun np, "mediatek,phy-mode");
112*4882a593Smuzhiyun if (IS_ERR(plat->mode)) {
113*4882a593Smuzhiyun dev_err(dev, "missing phy-mode phandle\n");
114*4882a593Smuzhiyun return PTR_ERR(plat->mode);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun regmap_update_bits(plat->mode, SYS_CFG, SYS_CFG_SATA_MSK,
118*4882a593Smuzhiyun SYS_CFG_SATA_EN);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun of_property_read_u32(np, "ports-implemented", &hpriv->force_port_map);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun return 0;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
mtk_ahci_probe(struct platform_device * pdev)126*4882a593Smuzhiyun static int mtk_ahci_probe(struct platform_device *pdev)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun struct device *dev = &pdev->dev;
129*4882a593Smuzhiyun struct mtk_ahci_plat *plat;
130*4882a593Smuzhiyun struct ahci_host_priv *hpriv;
131*4882a593Smuzhiyun int err;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun plat = devm_kzalloc(dev, sizeof(*plat), GFP_KERNEL);
134*4882a593Smuzhiyun if (!plat)
135*4882a593Smuzhiyun return -ENOMEM;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun hpriv = ahci_platform_get_resources(pdev, 0);
138*4882a593Smuzhiyun if (IS_ERR(hpriv))
139*4882a593Smuzhiyun return PTR_ERR(hpriv);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun hpriv->plat_data = plat;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun err = mtk_ahci_parse_property(hpriv, dev);
144*4882a593Smuzhiyun if (err)
145*4882a593Smuzhiyun return err;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun err = mtk_ahci_platform_resets(hpriv, dev);
148*4882a593Smuzhiyun if (err)
149*4882a593Smuzhiyun return err;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun err = ahci_platform_enable_resources(hpriv);
152*4882a593Smuzhiyun if (err)
153*4882a593Smuzhiyun return err;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun err = ahci_platform_init_host(pdev, hpriv, &ahci_port_info,
156*4882a593Smuzhiyun &ahci_platform_sht);
157*4882a593Smuzhiyun if (err)
158*4882a593Smuzhiyun goto disable_resources;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun return 0;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun disable_resources:
163*4882a593Smuzhiyun ahci_platform_disable_resources(hpriv);
164*4882a593Smuzhiyun return err;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(ahci_pm_ops, ahci_platform_suspend,
168*4882a593Smuzhiyun ahci_platform_resume);
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun static const struct of_device_id ahci_of_match[] = {
171*4882a593Smuzhiyun { .compatible = "mediatek,mtk-ahci", },
172*4882a593Smuzhiyun {},
173*4882a593Smuzhiyun };
174*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ahci_of_match);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun static struct platform_driver mtk_ahci_driver = {
177*4882a593Smuzhiyun .probe = mtk_ahci_probe,
178*4882a593Smuzhiyun .remove = ata_platform_remove_one,
179*4882a593Smuzhiyun .driver = {
180*4882a593Smuzhiyun .name = DRV_NAME,
181*4882a593Smuzhiyun .of_match_table = ahci_of_match,
182*4882a593Smuzhiyun .pm = &ahci_pm_ops,
183*4882a593Smuzhiyun },
184*4882a593Smuzhiyun };
185*4882a593Smuzhiyun module_platform_driver(mtk_ahci_driver);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun MODULE_DESCRIPTION("MediaTek SATA AHCI Driver");
188*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
189