1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 STMicroelectronics Limited
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>
15 #include <linux/ahci_platform.h>
16 #include <linux/libata.h>
17 #include <linux/reset.h>
19 #include <linux/dma-mapping.h>
23 #define DRV_NAME "st_ahci"
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
32 struct st_ahci_drv_data {
33 struct reset_control *pwr;
34 struct reset_control *sw_rst;
35 struct reset_control *pwr_rst;
38 static void st_ahci_configure_oob(void __iomem *mmio)
40 unsigned long old_val, new_val;
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);
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);
53 static int st_ahci_deassert_resets(struct ahci_host_priv *hpriv,
56 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
60 err = reset_control_deassert(drv_data->pwr);
62 dev_err(dev, "unable to bring out of pwrdwn\n");
67 if (drv_data->sw_rst) {
68 err = reset_control_deassert(drv_data->sw_rst);
70 dev_err(dev, "unable to bring out of sw-rst\n");
75 if (drv_data->pwr_rst) {
76 err = reset_control_deassert(drv_data->pwr_rst);
78 dev_err(dev, "unable to bring out of pwr-rst\n");
86 static void st_ahci_host_stop(struct ata_host *host)
88 struct ahci_host_priv *hpriv = host->private_data;
89 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
90 struct device *dev = host->dev;
94 err = reset_control_assert(drv_data->pwr);
96 dev_err(dev, "unable to pwrdwn\n");
99 ahci_platform_disable_resources(hpriv);
102 static int st_ahci_probe_resets(struct ahci_host_priv *hpriv,
105 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
107 drv_data->pwr = devm_reset_control_get(dev, "pwr-dwn");
108 if (IS_ERR(drv_data->pwr)) {
109 dev_info(dev, "power reset control not defined\n");
110 drv_data->pwr = NULL;
113 drv_data->sw_rst = devm_reset_control_get(dev, "sw-rst");
114 if (IS_ERR(drv_data->sw_rst)) {
115 dev_info(dev, "soft reset control not defined\n");
116 drv_data->sw_rst = NULL;
119 drv_data->pwr_rst = devm_reset_control_get(dev, "pwr-rst");
120 if (IS_ERR(drv_data->pwr_rst)) {
121 dev_dbg(dev, "power soft reset control not defined\n");
122 drv_data->pwr_rst = NULL;
125 return st_ahci_deassert_resets(hpriv, dev);
128 static struct ata_port_operations st_ahci_port_ops = {
129 .inherits = &ahci_platform_ops,
130 .host_stop = st_ahci_host_stop,
133 static const struct ata_port_info st_ahci_port_info = {
134 .flags = AHCI_FLAG_COMMON,
135 .pio_mask = ATA_PIO4,
136 .udma_mask = ATA_UDMA6,
137 .port_ops = &st_ahci_port_ops,
140 static const struct scsi_host_template ahci_platform_sht = {
144 static int st_ahci_probe(struct platform_device *pdev)
146 struct st_ahci_drv_data *drv_data;
147 struct ahci_host_priv *hpriv;
150 drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
154 hpriv = ahci_platform_get_resources(pdev, 0);
156 return PTR_ERR(hpriv);
157 hpriv->plat_data = drv_data;
159 err = st_ahci_probe_resets(hpriv, &pdev->dev);
163 err = ahci_platform_enable_resources(hpriv);
167 st_ahci_configure_oob(hpriv->mmio);
169 err = ahci_platform_init_host(pdev, hpriv, &st_ahci_port_info,
172 ahci_platform_disable_resources(hpriv);
179 static int st_ahci_suspend(struct device *dev)
181 struct ata_host *host = dev_get_drvdata(dev);
182 struct ahci_host_priv *hpriv = host->private_data;
183 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
186 err = ahci_platform_suspend_host(dev);
191 err = reset_control_assert(drv_data->pwr);
193 dev_err(dev, "unable to pwrdwn");
198 ahci_platform_disable_resources(hpriv);
203 static int st_ahci_resume(struct device *dev)
205 struct ata_host *host = dev_get_drvdata(dev);
206 struct ahci_host_priv *hpriv = host->private_data;
209 err = ahci_platform_enable_resources(hpriv);
213 err = st_ahci_deassert_resets(hpriv, dev);
215 ahci_platform_disable_resources(hpriv);
219 st_ahci_configure_oob(hpriv->mmio);
221 return ahci_platform_resume_host(dev);
224 static DEFINE_SIMPLE_DEV_PM_OPS(st_ahci_pm_ops, st_ahci_suspend, st_ahci_resume);
226 static const struct of_device_id st_ahci_match[] = {
227 { .compatible = "st,ahci", },
230 MODULE_DEVICE_TABLE(of, st_ahci_match);
232 static struct platform_driver st_ahci_driver = {
235 .pm = pm_sleep_ptr(&st_ahci_pm_ops),
236 .of_match_table = st_ahci_match,
238 .probe = st_ahci_probe,
239 .remove = ata_platform_remove_one,
241 module_platform_driver(st_ahci_driver);
245 MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver");
246 MODULE_LICENSE("GPL v2");