blob: 835d6eea84fdc3501bd16af13acea6bba1873f74 [file] [log] [blame]
Lee Jones76884cb2014-02-26 14:47:21 +00001/*
2 * Copyright (C) 2012 STMicroelectronics Limited
3 *
4 * Authors: Francesco Virlinzi <francesco.virlinzi@st.com>
5 * Alexandre Torgue <alexandre.torgue@st.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/export.h>
15#include <linux/platform_device.h>
16#include <linux/clk.h>
17#include <linux/of.h>
18#include <linux/ahci_platform.h>
Lee Jones76884cb2014-02-26 14:47:21 +000019#include <linux/libata.h>
20#include <linux/reset.h>
21#include <linux/io.h>
22#include <linux/dma-mapping.h>
23
24#include "ahci.h"
25
26#define ST_AHCI_OOBR 0xbc
27#define ST_AHCI_OOBR_WE BIT(31)
28#define ST_AHCI_OOBR_CWMIN_SHIFT 24
29#define ST_AHCI_OOBR_CWMAX_SHIFT 16
30#define ST_AHCI_OOBR_CIMIN_SHIFT 8
31#define ST_AHCI_OOBR_CIMAX_SHIFT 0
32
33struct st_ahci_drv_data {
34 struct platform_device *ahci;
Lee Jones76884cb2014-02-26 14:47:21 +000035 struct reset_control *pwr;
36 struct reset_control *sw_rst;
37 struct reset_control *pwr_rst;
38 struct ahci_host_priv *hpriv;
39};
40
41static void st_ahci_configure_oob(void __iomem *mmio)
42{
43 unsigned long old_val, new_val;
44
45 new_val = (0x02 << ST_AHCI_OOBR_CWMIN_SHIFT) |
46 (0x04 << ST_AHCI_OOBR_CWMAX_SHIFT) |
47 (0x08 << ST_AHCI_OOBR_CIMIN_SHIFT) |
48 (0x0C << ST_AHCI_OOBR_CIMAX_SHIFT);
49
50 old_val = readl(mmio + ST_AHCI_OOBR);
51 writel(old_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
52 writel(new_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
53 writel(new_val, mmio + ST_AHCI_OOBR);
54}
55
56static int st_ahci_deassert_resets(struct device *dev)
57{
58 struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
59 int err;
60
61 if (drv_data->pwr) {
62 err = reset_control_deassert(drv_data->pwr);
63 if (err) {
64 dev_err(dev, "unable to bring out of pwrdwn\n");
65 return err;
66 }
67 }
68
69 st_ahci_configure_oob(drv_data->hpriv->mmio);
70
71 if (drv_data->sw_rst) {
72 err = reset_control_deassert(drv_data->sw_rst);
73 if (err) {
74 dev_err(dev, "unable to bring out of sw-rst\n");
75 return err;
76 }
77 }
78
79 if (drv_data->pwr_rst) {
80 err = reset_control_deassert(drv_data->pwr_rst);
81 if (err) {
82 dev_err(dev, "unable to bring out of pwr-rst\n");
83 return err;
84 }
85 }
86
87 return 0;
88}
89
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +010090static void st_ahci_host_stop(struct ata_host *host)
Lee Jonesa8237082014-03-12 12:39:40 +000091{
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +010092 struct ahci_host_priv *hpriv = host->private_data;
93 struct device *dev = host->dev;
Lee Jonesa8237082014-03-12 12:39:40 +000094 struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
Lee Jonesa8237082014-03-12 12:39:40 +000095 int err;
96
97 if (drv_data->pwr) {
98 err = reset_control_assert(drv_data->pwr);
99 if (err)
Bartlomiej Zolnierkiewicz33081b32014-03-14 19:20:58 +0100100 dev_err(dev, "unable to pwrdwn\n");
Lee Jonesa8237082014-03-12 12:39:40 +0000101 }
102
103 ahci_platform_disable_resources(hpriv);
Lee Jonesa8237082014-03-12 12:39:40 +0000104}
105
Lee Jones76884cb2014-02-26 14:47:21 +0000106static int st_ahci_probe_resets(struct platform_device *pdev)
107{
108 struct st_ahci_drv_data *drv_data = platform_get_drvdata(pdev);
109
110 drv_data->pwr = devm_reset_control_get(&pdev->dev, "pwr-dwn");
111 if (IS_ERR(drv_data->pwr)) {
112 dev_info(&pdev->dev, "power reset control not defined\n");
113 drv_data->pwr = NULL;
114 }
115
116 drv_data->sw_rst = devm_reset_control_get(&pdev->dev, "sw-rst");
117 if (IS_ERR(drv_data->sw_rst)) {
118 dev_info(&pdev->dev, "soft reset control not defined\n");
119 drv_data->sw_rst = NULL;
120 }
121
122 drv_data->pwr_rst = devm_reset_control_get(&pdev->dev, "pwr-rst");
123 if (IS_ERR(drv_data->pwr_rst)) {
124 dev_dbg(&pdev->dev, "power soft reset control not defined\n");
125 drv_data->pwr_rst = NULL;
126 }
127
128 return st_ahci_deassert_resets(&pdev->dev);
129}
130
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +0100131static struct ata_port_operations st_ahci_port_ops = {
132 .inherits = &ahci_platform_ops,
133 .host_stop = st_ahci_host_stop,
134};
135
Lee Jones76884cb2014-02-26 14:47:21 +0000136static const struct ata_port_info st_ahci_port_info = {
137 .flags = AHCI_FLAG_COMMON,
138 .pio_mask = ATA_PIO4,
139 .udma_mask = ATA_UDMA6,
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +0100140 .port_ops = &st_ahci_port_ops,
Lee Jones76884cb2014-02-26 14:47:21 +0000141};
142
143static int st_ahci_probe(struct platform_device *pdev)
144{
145 struct st_ahci_drv_data *drv_data;
146 struct ahci_host_priv *hpriv;
147 int err;
148
149 drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
150 if (!drv_data)
151 return -ENOMEM;
152
153 platform_set_drvdata(pdev, drv_data);
154
155 hpriv = ahci_platform_get_resources(pdev);
156 if (IS_ERR(hpriv))
157 return PTR_ERR(hpriv);
158
159 drv_data->hpriv = hpriv;
160
161 err = st_ahci_probe_resets(pdev);
162 if (err)
163 return err;
164
165 err = ahci_platform_enable_resources(hpriv);
166 if (err)
167 return err;
168
Antoine Ténart725c7b52014-07-30 20:13:56 +0200169 err = ahci_platform_init_host(pdev, hpriv, &st_ahci_port_info);
Lee Jones76884cb2014-02-26 14:47:21 +0000170 if (err) {
171 ahci_platform_disable_resources(hpriv);
172 return err;
173 }
174
175 return 0;
176}
177
Lee Jones76884cb2014-02-26 14:47:21 +0000178#ifdef CONFIG_PM_SLEEP
179static int st_ahci_suspend(struct device *dev)
180{
181 struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
182 struct ahci_host_priv *hpriv = drv_data->hpriv;
183 int err;
184
Bartlomiej Zolnierkiewicz33081b32014-03-14 19:20:58 +0100185 err = ahci_platform_suspend_host(dev);
186 if (err)
187 return err;
Lee Jones761a8c22014-03-12 12:39:42 +0000188
Lee Jones76884cb2014-02-26 14:47:21 +0000189 if (drv_data->pwr) {
190 err = reset_control_assert(drv_data->pwr);
191 if (err) {
192 dev_err(dev, "unable to pwrdwn");
193 return err;
194 }
195 }
196
197 ahci_platform_disable_resources(hpriv);
198
199 return 0;
200}
201
202static int st_ahci_resume(struct device *dev)
203{
204 struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
205 struct ahci_host_priv *hpriv = drv_data->hpriv;
206 int err;
207
208 err = ahci_platform_enable_resources(hpriv);
209 if (err)
210 return err;
211
212 err = st_ahci_deassert_resets(dev);
213 if (err) {
214 ahci_platform_disable_resources(hpriv);
215 return err;
216 }
217
Lee Jones761a8c22014-03-12 12:39:42 +0000218 return ahci_platform_resume_host(dev);
Lee Jones76884cb2014-02-26 14:47:21 +0000219}
220#endif
221
222static SIMPLE_DEV_PM_OPS(st_ahci_pm_ops, st_ahci_suspend, st_ahci_resume);
223
Kiran Padwal09de99d2014-07-22 19:45:43 +0530224static const struct of_device_id st_ahci_match[] = {
Lee Jones76884cb2014-02-26 14:47:21 +0000225 { .compatible = "st,ahci", },
226 {},
227};
228MODULE_DEVICE_TABLE(of, st_ahci_match);
229
230static struct platform_driver st_ahci_driver = {
231 .driver = {
232 .name = "st_ahci",
233 .owner = THIS_MODULE,
234 .pm = &st_ahci_pm_ops,
235 .of_match_table = of_match_ptr(st_ahci_match),
236 },
237 .probe = st_ahci_probe,
Lee Jonesa8237082014-03-12 12:39:40 +0000238 .remove = ata_platform_remove_one,
Lee Jones76884cb2014-02-26 14:47:21 +0000239};
240module_platform_driver(st_ahci_driver);
241
242MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
243MODULE_AUTHOR("Francesco Virlinzi <francesco.virlinzi@st.com>");
Lee Jones4a2e5122014-03-12 12:39:38 +0000244MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver");
Lee Jones76884cb2014-02-26 14:47:21 +0000245MODULE_LICENSE("GPL v2");