blob: a28532aad97b960fba77689701d40c72aacbe6f9 [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
90static int st_ahci_probe_resets(struct platform_device *pdev)
91{
92 struct st_ahci_drv_data *drv_data = platform_get_drvdata(pdev);
93
94 drv_data->pwr = devm_reset_control_get(&pdev->dev, "pwr-dwn");
95 if (IS_ERR(drv_data->pwr)) {
96 dev_info(&pdev->dev, "power reset control not defined\n");
97 drv_data->pwr = NULL;
98 }
99
100 drv_data->sw_rst = devm_reset_control_get(&pdev->dev, "sw-rst");
101 if (IS_ERR(drv_data->sw_rst)) {
102 dev_info(&pdev->dev, "soft reset control not defined\n");
103 drv_data->sw_rst = NULL;
104 }
105
106 drv_data->pwr_rst = devm_reset_control_get(&pdev->dev, "pwr-rst");
107 if (IS_ERR(drv_data->pwr_rst)) {
108 dev_dbg(&pdev->dev, "power soft reset control not defined\n");
109 drv_data->pwr_rst = NULL;
110 }
111
112 return st_ahci_deassert_resets(&pdev->dev);
113}
114
115static const struct ata_port_info st_ahci_port_info = {
116 .flags = AHCI_FLAG_COMMON,
117 .pio_mask = ATA_PIO4,
118 .udma_mask = ATA_UDMA6,
119 .port_ops = &ahci_platform_ops,
120};
121
122static int st_ahci_probe(struct platform_device *pdev)
123{
124 struct st_ahci_drv_data *drv_data;
125 struct ahci_host_priv *hpriv;
126 int err;
127
128 drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
129 if (!drv_data)
130 return -ENOMEM;
131
132 platform_set_drvdata(pdev, drv_data);
133
134 hpriv = ahci_platform_get_resources(pdev);
135 if (IS_ERR(hpriv))
136 return PTR_ERR(hpriv);
137
138 drv_data->hpriv = hpriv;
139
140 err = st_ahci_probe_resets(pdev);
141 if (err)
142 return err;
143
144 err = ahci_platform_enable_resources(hpriv);
145 if (err)
146 return err;
147
148 err = ahci_platform_init_host(pdev, hpriv, &st_ahci_port_info, 0, 0);
149 if (err) {
150 ahci_platform_disable_resources(hpriv);
151 return err;
152 }
153
154 return 0;
155}
156
157static int st_ahci_remove(struct platform_device *pdev)
158{
159 struct st_ahci_drv_data *drv_data = platform_get_drvdata(pdev);
160 struct ahci_host_priv *hpriv = drv_data->hpriv;
161 int err;
162
163 if (drv_data->pwr) {
164 err = reset_control_assert(drv_data->pwr);
165 if (err)
166 dev_err(&pdev->dev, "unable to pwrdwn\n");
167 }
168
169 ahci_platform_disable_resources(hpriv);
170
171 return 0;
172}
173
174#ifdef CONFIG_PM_SLEEP
175static int st_ahci_suspend(struct device *dev)
176{
177 struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
178 struct ahci_host_priv *hpriv = drv_data->hpriv;
179 int err;
180
181 if (drv_data->pwr) {
182 err = reset_control_assert(drv_data->pwr);
183 if (err) {
184 dev_err(dev, "unable to pwrdwn");
185 return err;
186 }
187 }
188
189 ahci_platform_disable_resources(hpriv);
190
191 return 0;
192}
193
194static int st_ahci_resume(struct device *dev)
195{
196 struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
197 struct ahci_host_priv *hpriv = drv_data->hpriv;
198 int err;
199
200 err = ahci_platform_enable_resources(hpriv);
201 if (err)
202 return err;
203
204 err = st_ahci_deassert_resets(dev);
205 if (err) {
206 ahci_platform_disable_resources(hpriv);
207 return err;
208 }
209
210 return 0;
211}
212#endif
213
214static SIMPLE_DEV_PM_OPS(st_ahci_pm_ops, st_ahci_suspend, st_ahci_resume);
215
216static struct of_device_id st_ahci_match[] = {
217 { .compatible = "st,ahci", },
218 {},
219};
220MODULE_DEVICE_TABLE(of, st_ahci_match);
221
222static struct platform_driver st_ahci_driver = {
223 .driver = {
224 .name = "st_ahci",
225 .owner = THIS_MODULE,
226 .pm = &st_ahci_pm_ops,
227 .of_match_table = of_match_ptr(st_ahci_match),
228 },
229 .probe = st_ahci_probe,
230 .remove = st_ahci_remove,
231};
232module_platform_driver(st_ahci_driver);
233
234MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
235MODULE_AUTHOR("Francesco Virlinzi <francesco.virlinzi@st.com>");
Lee Jones4a2e5122014-03-12 12:39:38 +0000236MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver");
Lee Jones76884cb2014-02-26 14:47:21 +0000237MODULE_LICENSE("GPL v2");