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