xref: /linux/drivers/ata/ahci_st.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 STMicroelectronics Limited
4  *
5  * Authors: Francesco Virlinzi <francesco.virlinzi@st.com>
6  *	    Alexandre Torgue <alexandre.torgue@st.com>
7  */
8 
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/export.h>
12 #include <linux/platform_device.h>
13 #include <linux/clk.h>
14 #include <linux/of.h>
15 #include <linux/ahci_platform.h>
16 #include <linux/libata.h>
17 #include <linux/reset.h>
18 #include <linux/io.h>
19 #include <linux/dma-mapping.h>
20 
21 #include "ahci.h"
22 
23 #define DRV_NAME  "st_ahci"
24 
25 #define ST_AHCI_OOBR			0xbc
26 #define ST_AHCI_OOBR_WE			BIT(31)
27 #define ST_AHCI_OOBR_CWMIN_SHIFT	24
28 #define ST_AHCI_OOBR_CWMAX_SHIFT	16
29 #define ST_AHCI_OOBR_CIMIN_SHIFT	8
30 #define ST_AHCI_OOBR_CIMAX_SHIFT	0
31 
32 struct st_ahci_drv_data {
33 	struct reset_control *pwr;
34 	struct reset_control *sw_rst;
35 	struct reset_control *pwr_rst;
36 };
37 
38 static void st_ahci_configure_oob(void __iomem *mmio)
39 {
40 	unsigned long old_val, new_val;
41 
42 	new_val = (0x02 << ST_AHCI_OOBR_CWMIN_SHIFT) |
43 		  (0x04 << ST_AHCI_OOBR_CWMAX_SHIFT) |
44 		  (0x08 << ST_AHCI_OOBR_CIMIN_SHIFT) |
45 		  (0x0C << ST_AHCI_OOBR_CIMAX_SHIFT);
46 
47 	old_val = readl(mmio + ST_AHCI_OOBR);
48 	writel(old_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
49 	writel(new_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
50 	writel(new_val, mmio + ST_AHCI_OOBR);
51 }
52 
53 static int st_ahci_deassert_resets(struct ahci_host_priv *hpriv,
54 				struct device *dev)
55 {
56 	struct st_ahci_drv_data *drv_data = hpriv->plat_data;
57 	int err;
58 
59 	if (drv_data->pwr) {
60 		err = reset_control_deassert(drv_data->pwr);
61 		if (err) {
62 			dev_err(dev, "unable to bring out of pwrdwn\n");
63 			return err;
64 		}
65 	}
66 
67 	if (drv_data->sw_rst) {
68 		err = reset_control_deassert(drv_data->sw_rst);
69 		if (err) {
70 			dev_err(dev, "unable to bring out of sw-rst\n");
71 			return err;
72 		}
73 	}
74 
75 	if (drv_data->pwr_rst) {
76 		err = reset_control_deassert(drv_data->pwr_rst);
77 		if (err) {
78 			dev_err(dev, "unable to bring out of pwr-rst\n");
79 			return err;
80 		}
81 	}
82 
83 	return 0;
84 }
85 
86 static int st_ahci_assert_pwrdwn(struct ahci_host_priv *hpriv,
87 				 struct device *dev)
88 {
89 	struct st_ahci_drv_data *drv_data = hpriv->plat_data;
90 	int err;
91 
92 	if (!drv_data->pwr)
93 		return 0;
94 
95 	err = reset_control_assert(drv_data->pwr);
96 	if (err)
97 		dev_err(dev, "unable to pwrdwn\n");
98 
99 	return err;
100 }
101 
102 static void st_ahci_host_stop(struct ata_host *host)
103 {
104 	struct ahci_host_priv *hpriv = host->private_data;
105 
106 	st_ahci_assert_pwrdwn(hpriv, host->dev);
107 
108 	ahci_platform_disable_resources(hpriv);
109 }
110 
111 static int st_ahci_probe_resets(struct ahci_host_priv *hpriv,
112 				struct device *dev)
113 {
114 	struct st_ahci_drv_data *drv_data = hpriv->plat_data;
115 
116 	drv_data->pwr = devm_reset_control_get_optional(dev, "pwr-dwn");
117 	if (IS_ERR(drv_data->pwr))
118 		return dev_err_probe(dev, PTR_ERR(drv_data->pwr),
119 				     "failed to get pwr-dwn reset\n");
120 
121 	drv_data->sw_rst = devm_reset_control_get_optional(dev, "sw-rst");
122 	if (IS_ERR(drv_data->sw_rst))
123 		return dev_err_probe(dev, PTR_ERR(drv_data->sw_rst),
124 				     "failed to get sw-rst reset\n");
125 
126 	drv_data->pwr_rst = devm_reset_control_get_optional(dev, "pwr-rst");
127 	if (IS_ERR(drv_data->pwr_rst))
128 		return dev_err_probe(dev, PTR_ERR(drv_data->pwr_rst),
129 				     "failed to get pwr-rst reset\n");
130 
131 	return st_ahci_deassert_resets(hpriv, dev);
132 }
133 
134 static struct ata_port_operations st_ahci_port_ops = {
135 	.inherits	= &ahci_platform_ops,
136 	.host_stop	= st_ahci_host_stop,
137 };
138 
139 static const struct ata_port_info st_ahci_port_info = {
140 	.flags          = AHCI_FLAG_COMMON,
141 	.pio_mask       = ATA_PIO4,
142 	.udma_mask      = ATA_UDMA6,
143 	.port_ops       = &st_ahci_port_ops,
144 };
145 
146 static const struct scsi_host_template ahci_platform_sht = {
147 	AHCI_SHT(DRV_NAME),
148 };
149 
150 static int st_ahci_probe(struct platform_device *pdev)
151 {
152 	struct st_ahci_drv_data *drv_data;
153 	struct ahci_host_priv *hpriv;
154 	int err;
155 
156 	drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
157 	if (!drv_data)
158 		return -ENOMEM;
159 
160 	hpriv = ahci_platform_get_resources(pdev, 0);
161 	if (IS_ERR(hpriv))
162 		return PTR_ERR(hpriv);
163 	hpriv->plat_data = drv_data;
164 
165 	err = st_ahci_probe_resets(hpriv, &pdev->dev);
166 	if (err)
167 		return err;
168 
169 	err = ahci_platform_enable_resources(hpriv);
170 	if (err)
171 		goto assert_pwrdwn;
172 
173 	st_ahci_configure_oob(hpriv->mmio);
174 
175 	err = ahci_platform_init_host(pdev, hpriv, &st_ahci_port_info,
176 				      &ahci_platform_sht);
177 	if (err)
178 		goto disable_resources;
179 
180 	return 0;
181 
182 disable_resources:
183 	ahci_platform_disable_resources(hpriv);
184 assert_pwrdwn:
185 	st_ahci_assert_pwrdwn(hpriv, &pdev->dev);
186 
187 	return err;
188 }
189 
190 static int st_ahci_suspend(struct device *dev)
191 {
192 	struct ata_host *host = dev_get_drvdata(dev);
193 	struct ahci_host_priv *hpriv = host->private_data;
194 	int err;
195 
196 	err = ahci_platform_suspend_host(dev);
197 	if (err)
198 		return err;
199 
200 	err = st_ahci_assert_pwrdwn(hpriv, dev);
201 	if (err)
202 		return err;
203 
204 	ahci_platform_disable_resources(hpriv);
205 
206 	return 0;
207 }
208 
209 static int st_ahci_resume(struct device *dev)
210 {
211 	struct ata_host *host = dev_get_drvdata(dev);
212 	struct ahci_host_priv *hpriv = host->private_data;
213 	int err;
214 
215 	err = ahci_platform_enable_resources(hpriv);
216 	if (err)
217 		return err;
218 
219 	err = st_ahci_deassert_resets(hpriv, dev);
220 	if (err) {
221 		ahci_platform_disable_resources(hpriv);
222 		return err;
223 	}
224 
225 	st_ahci_configure_oob(hpriv->mmio);
226 
227 	return ahci_platform_resume_host(dev);
228 }
229 
230 static DEFINE_SIMPLE_DEV_PM_OPS(st_ahci_pm_ops, st_ahci_suspend, st_ahci_resume);
231 
232 static const struct of_device_id st_ahci_match[] = {
233 	{ .compatible = "st,ahci", },
234 	{ /* sentinel */ }
235 };
236 MODULE_DEVICE_TABLE(of, st_ahci_match);
237 
238 static struct platform_driver st_ahci_driver = {
239 	.driver = {
240 		.name = DRV_NAME,
241 		.pm = pm_sleep_ptr(&st_ahci_pm_ops),
242 		.of_match_table = st_ahci_match,
243 	},
244 	.probe = st_ahci_probe,
245 	.remove = ata_platform_remove_one,
246 };
247 module_platform_driver(st_ahci_driver);
248 
249 MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
250 MODULE_AUTHOR("Francesco Virlinzi <francesco.virlinzi@st.com>");
251 MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver");
252 MODULE_LICENSE("GPL v2");
253