blob: c268264c2129c52abd9faa671cf97e18c4bb3569 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Lee Jones76884cb2014-02-26 14:47:21 +00002/*
3 * Copyright (C) 2012 STMicroelectronics Limited
4 *
5 * Authors: Francesco Virlinzi <francesco.virlinzi@st.com>
6 * Alexandre Torgue <alexandre.torgue@st.com>
Lee Jones76884cb2014-02-26 14:47:21 +00007 */
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>
Lee Jones76884cb2014-02-26 14:47:21 +000016#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
Akinobu Mita018d5ef2015-01-29 08:30:29 +090023#define DRV_NAME "st_ahci"
24
Lee Jones76884cb2014-02-26 14:47:21 +000025#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
32struct st_ahci_drv_data {
33 struct platform_device *ahci;
Lee Jones76884cb2014-02-26 14:47:21 +000034 struct reset_control *pwr;
35 struct reset_control *sw_rst;
36 struct reset_control *pwr_rst;
Lee Jones76884cb2014-02-26 14:47:21 +000037};
38
39static void st_ahci_configure_oob(void __iomem *mmio)
40{
41 unsigned long old_val, new_val;
42
43 new_val = (0x02 << ST_AHCI_OOBR_CWMIN_SHIFT) |
44 (0x04 << ST_AHCI_OOBR_CWMAX_SHIFT) |
45 (0x08 << ST_AHCI_OOBR_CIMIN_SHIFT) |
46 (0x0C << ST_AHCI_OOBR_CIMAX_SHIFT);
47
48 old_val = readl(mmio + ST_AHCI_OOBR);
49 writel(old_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
50 writel(new_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
51 writel(new_val, mmio + ST_AHCI_OOBR);
52}
53
Peter Griffine0e26742015-04-20 14:41:05 +010054static int st_ahci_deassert_resets(struct ahci_host_priv *hpriv,
55 struct device *dev)
Lee Jones76884cb2014-02-26 14:47:21 +000056{
Peter Griffine0e26742015-04-20 14:41:05 +010057 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
Lee Jones76884cb2014-02-26 14:47:21 +000058 int err;
59
60 if (drv_data->pwr) {
61 err = reset_control_deassert(drv_data->pwr);
62 if (err) {
63 dev_err(dev, "unable to bring out of pwrdwn\n");
64 return err;
65 }
66 }
67
Lee Jones76884cb2014-02-26 14:47:21 +000068 if (drv_data->sw_rst) {
69 err = reset_control_deassert(drv_data->sw_rst);
70 if (err) {
71 dev_err(dev, "unable to bring out of sw-rst\n");
72 return err;
73 }
74 }
75
76 if (drv_data->pwr_rst) {
77 err = reset_control_deassert(drv_data->pwr_rst);
78 if (err) {
79 dev_err(dev, "unable to bring out of pwr-rst\n");
80 return err;
81 }
82 }
83
84 return 0;
85}
86
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +010087static void st_ahci_host_stop(struct ata_host *host)
Lee Jonesa8237082014-03-12 12:39:40 +000088{
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +010089 struct ahci_host_priv *hpriv = host->private_data;
Peter Griffine0e26742015-04-20 14:41:05 +010090 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +010091 struct device *dev = host->dev;
Lee Jonesa8237082014-03-12 12:39:40 +000092 int err;
93
94 if (drv_data->pwr) {
95 err = reset_control_assert(drv_data->pwr);
96 if (err)
Bartlomiej Zolnierkiewicz33081b32014-03-14 19:20:58 +010097 dev_err(dev, "unable to pwrdwn\n");
Lee Jonesa8237082014-03-12 12:39:40 +000098 }
99
100 ahci_platform_disable_resources(hpriv);
Lee Jonesa8237082014-03-12 12:39:40 +0000101}
102
Peter Griffine0e26742015-04-20 14:41:05 +0100103static int st_ahci_probe_resets(struct ahci_host_priv *hpriv,
104 struct device *dev)
Lee Jones76884cb2014-02-26 14:47:21 +0000105{
Peter Griffine0e26742015-04-20 14:41:05 +0100106 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
Lee Jones76884cb2014-02-26 14:47:21 +0000107
Peter Griffine0e26742015-04-20 14:41:05 +0100108 drv_data->pwr = devm_reset_control_get(dev, "pwr-dwn");
Lee Jones76884cb2014-02-26 14:47:21 +0000109 if (IS_ERR(drv_data->pwr)) {
Peter Griffine0e26742015-04-20 14:41:05 +0100110 dev_info(dev, "power reset control not defined\n");
Lee Jones76884cb2014-02-26 14:47:21 +0000111 drv_data->pwr = NULL;
112 }
113
Peter Griffine0e26742015-04-20 14:41:05 +0100114 drv_data->sw_rst = devm_reset_control_get(dev, "sw-rst");
Lee Jones76884cb2014-02-26 14:47:21 +0000115 if (IS_ERR(drv_data->sw_rst)) {
Peter Griffine0e26742015-04-20 14:41:05 +0100116 dev_info(dev, "soft reset control not defined\n");
Lee Jones76884cb2014-02-26 14:47:21 +0000117 drv_data->sw_rst = NULL;
118 }
119
Peter Griffine0e26742015-04-20 14:41:05 +0100120 drv_data->pwr_rst = devm_reset_control_get(dev, "pwr-rst");
Lee Jones76884cb2014-02-26 14:47:21 +0000121 if (IS_ERR(drv_data->pwr_rst)) {
Peter Griffine0e26742015-04-20 14:41:05 +0100122 dev_dbg(dev, "power soft reset control not defined\n");
Lee Jones76884cb2014-02-26 14:47:21 +0000123 drv_data->pwr_rst = NULL;
124 }
125
Peter Griffine0e26742015-04-20 14:41:05 +0100126 return st_ahci_deassert_resets(hpriv, dev);
Lee Jones76884cb2014-02-26 14:47:21 +0000127}
128
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +0100129static struct ata_port_operations st_ahci_port_ops = {
130 .inherits = &ahci_platform_ops,
131 .host_stop = st_ahci_host_stop,
132};
133
Lee Jones76884cb2014-02-26 14:47:21 +0000134static const struct ata_port_info st_ahci_port_info = {
135 .flags = AHCI_FLAG_COMMON,
136 .pio_mask = ATA_PIO4,
137 .udma_mask = ATA_UDMA6,
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +0100138 .port_ops = &st_ahci_port_ops,
Lee Jones76884cb2014-02-26 14:47:21 +0000139};
140
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900141static struct scsi_host_template ahci_platform_sht = {
142 AHCI_SHT(DRV_NAME),
143};
144
Lee Jones76884cb2014-02-26 14:47:21 +0000145static int st_ahci_probe(struct platform_device *pdev)
146{
Patrice Chotarde7190692016-08-10 17:37:29 +0200147 struct device *dev = &pdev->dev;
Lee Jones76884cb2014-02-26 14:47:21 +0000148 struct st_ahci_drv_data *drv_data;
149 struct ahci_host_priv *hpriv;
150 int err;
151
152 drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
153 if (!drv_data)
154 return -ENOMEM;
155
Kunihiko Hayashi16af2d62018-08-22 21:13:01 +0900156 hpriv = ahci_platform_get_resources(pdev, 0);
Lee Jones76884cb2014-02-26 14:47:21 +0000157 if (IS_ERR(hpriv))
158 return PTR_ERR(hpriv);
Peter Griffine0e26742015-04-20 14:41:05 +0100159 hpriv->plat_data = drv_data;
Lee Jones76884cb2014-02-26 14:47:21 +0000160
Peter Griffine0e26742015-04-20 14:41:05 +0100161 err = st_ahci_probe_resets(hpriv, &pdev->dev);
Lee Jones76884cb2014-02-26 14:47:21 +0000162 if (err)
163 return err;
164
165 err = ahci_platform_enable_resources(hpriv);
166 if (err)
167 return err;
168
Peter Griffine0e26742015-04-20 14:41:05 +0100169 st_ahci_configure_oob(hpriv->mmio);
Peter Griffin9a1e75e2015-03-31 08:35:09 +0100170
Patrice Chotarde7190692016-08-10 17:37:29 +0200171 of_property_read_u32(dev->of_node,
172 "ports-implemented", &hpriv->force_port_map);
173
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900174 err = ahci_platform_init_host(pdev, hpriv, &st_ahci_port_info,
175 &ahci_platform_sht);
Lee Jones76884cb2014-02-26 14:47:21 +0000176 if (err) {
177 ahci_platform_disable_resources(hpriv);
178 return err;
179 }
180
181 return 0;
182}
183
Lee Jones76884cb2014-02-26 14:47:21 +0000184#ifdef CONFIG_PM_SLEEP
185static int st_ahci_suspend(struct device *dev)
186{
Peter Griffine0e26742015-04-20 14:41:05 +0100187 struct ata_host *host = dev_get_drvdata(dev);
188 struct ahci_host_priv *hpriv = host->private_data;
189 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
Lee Jones76884cb2014-02-26 14:47:21 +0000190 int err;
191
Bartlomiej Zolnierkiewicz33081b32014-03-14 19:20:58 +0100192 err = ahci_platform_suspend_host(dev);
193 if (err)
194 return err;
Lee Jones761a8c22014-03-12 12:39:42 +0000195
Lee Jones76884cb2014-02-26 14:47:21 +0000196 if (drv_data->pwr) {
197 err = reset_control_assert(drv_data->pwr);
198 if (err) {
199 dev_err(dev, "unable to pwrdwn");
200 return err;
201 }
202 }
203
204 ahci_platform_disable_resources(hpriv);
205
206 return 0;
207}
208
209static int st_ahci_resume(struct device *dev)
210{
Peter Griffine0e26742015-04-20 14:41:05 +0100211 struct ata_host *host = dev_get_drvdata(dev);
212 struct ahci_host_priv *hpriv = host->private_data;
Lee Jones76884cb2014-02-26 14:47:21 +0000213 int err;
214
215 err = ahci_platform_enable_resources(hpriv);
216 if (err)
217 return err;
218
Peter Griffine0e26742015-04-20 14:41:05 +0100219 err = st_ahci_deassert_resets(hpriv, dev);
Lee Jones76884cb2014-02-26 14:47:21 +0000220 if (err) {
221 ahci_platform_disable_resources(hpriv);
222 return err;
223 }
224
Peter Griffine0e26742015-04-20 14:41:05 +0100225 st_ahci_configure_oob(hpriv->mmio);
Peter Griffin9a1e75e2015-03-31 08:35:09 +0100226
Lee Jones761a8c22014-03-12 12:39:42 +0000227 return ahci_platform_resume_host(dev);
Lee Jones76884cb2014-02-26 14:47:21 +0000228}
229#endif
230
231static SIMPLE_DEV_PM_OPS(st_ahci_pm_ops, st_ahci_suspend, st_ahci_resume);
232
Kiran Padwal09de99d2014-07-22 19:45:43 +0530233static const struct of_device_id st_ahci_match[] = {
Lee Jones76884cb2014-02-26 14:47:21 +0000234 { .compatible = "st,ahci", },
235 {},
236};
237MODULE_DEVICE_TABLE(of, st_ahci_match);
238
239static struct platform_driver st_ahci_driver = {
240 .driver = {
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900241 .name = DRV_NAME,
Lee Jones76884cb2014-02-26 14:47:21 +0000242 .pm = &st_ahci_pm_ops,
243 .of_match_table = of_match_ptr(st_ahci_match),
244 },
245 .probe = st_ahci_probe,
Lee Jonesa8237082014-03-12 12:39:40 +0000246 .remove = ata_platform_remove_one,
Lee Jones76884cb2014-02-26 14:47:21 +0000247};
248module_platform_driver(st_ahci_driver);
249
250MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
251MODULE_AUTHOR("Francesco Virlinzi <francesco.virlinzi@st.com>");
Lee Jones4a2e5122014-03-12 12:39:38 +0000252MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver");
Lee Jones76884cb2014-02-26 14:47:21 +0000253MODULE_LICENSE("GPL v2");