blob: bc971af262e75f89cb64af2b408fb6a7ab0fdb03 [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
Akinobu Mita018d5ef2015-01-29 08:30:29 +090026#define DRV_NAME "st_ahci"
27
Lee Jones76884cb2014-02-26 14:47:21 +000028#define ST_AHCI_OOBR 0xbc
29#define ST_AHCI_OOBR_WE BIT(31)
30#define ST_AHCI_OOBR_CWMIN_SHIFT 24
31#define ST_AHCI_OOBR_CWMAX_SHIFT 16
32#define ST_AHCI_OOBR_CIMIN_SHIFT 8
33#define ST_AHCI_OOBR_CIMAX_SHIFT 0
34
35struct st_ahci_drv_data {
36 struct platform_device *ahci;
Lee Jones76884cb2014-02-26 14:47:21 +000037 struct reset_control *pwr;
38 struct reset_control *sw_rst;
39 struct reset_control *pwr_rst;
40 struct ahci_host_priv *hpriv;
41};
42
43static void st_ahci_configure_oob(void __iomem *mmio)
44{
45 unsigned long old_val, new_val;
46
47 new_val = (0x02 << ST_AHCI_OOBR_CWMIN_SHIFT) |
48 (0x04 << ST_AHCI_OOBR_CWMAX_SHIFT) |
49 (0x08 << ST_AHCI_OOBR_CIMIN_SHIFT) |
50 (0x0C << ST_AHCI_OOBR_CIMAX_SHIFT);
51
52 old_val = readl(mmio + ST_AHCI_OOBR);
53 writel(old_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
54 writel(new_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
55 writel(new_val, mmio + ST_AHCI_OOBR);
56}
57
58static int st_ahci_deassert_resets(struct device *dev)
59{
60 struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
61 int err;
62
63 if (drv_data->pwr) {
64 err = reset_control_deassert(drv_data->pwr);
65 if (err) {
66 dev_err(dev, "unable to bring out of pwrdwn\n");
67 return err;
68 }
69 }
70
71 st_ahci_configure_oob(drv_data->hpriv->mmio);
72
73 if (drv_data->sw_rst) {
74 err = reset_control_deassert(drv_data->sw_rst);
75 if (err) {
76 dev_err(dev, "unable to bring out of sw-rst\n");
77 return err;
78 }
79 }
80
81 if (drv_data->pwr_rst) {
82 err = reset_control_deassert(drv_data->pwr_rst);
83 if (err) {
84 dev_err(dev, "unable to bring out of pwr-rst\n");
85 return err;
86 }
87 }
88
89 return 0;
90}
91
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +010092static void st_ahci_host_stop(struct ata_host *host)
Lee Jonesa8237082014-03-12 12:39:40 +000093{
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +010094 struct ahci_host_priv *hpriv = host->private_data;
95 struct device *dev = host->dev;
Lee Jonesa8237082014-03-12 12:39:40 +000096 struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
Lee Jonesa8237082014-03-12 12:39:40 +000097 int err;
98
99 if (drv_data->pwr) {
100 err = reset_control_assert(drv_data->pwr);
101 if (err)
Bartlomiej Zolnierkiewicz33081b32014-03-14 19:20:58 +0100102 dev_err(dev, "unable to pwrdwn\n");
Lee Jonesa8237082014-03-12 12:39:40 +0000103 }
104
105 ahci_platform_disable_resources(hpriv);
Lee Jonesa8237082014-03-12 12:39:40 +0000106}
107
Lee Jones76884cb2014-02-26 14:47:21 +0000108static int st_ahci_probe_resets(struct platform_device *pdev)
109{
110 struct st_ahci_drv_data *drv_data = platform_get_drvdata(pdev);
111
112 drv_data->pwr = devm_reset_control_get(&pdev->dev, "pwr-dwn");
113 if (IS_ERR(drv_data->pwr)) {
114 dev_info(&pdev->dev, "power reset control not defined\n");
115 drv_data->pwr = NULL;
116 }
117
118 drv_data->sw_rst = devm_reset_control_get(&pdev->dev, "sw-rst");
119 if (IS_ERR(drv_data->sw_rst)) {
120 dev_info(&pdev->dev, "soft reset control not defined\n");
121 drv_data->sw_rst = NULL;
122 }
123
124 drv_data->pwr_rst = devm_reset_control_get(&pdev->dev, "pwr-rst");
125 if (IS_ERR(drv_data->pwr_rst)) {
126 dev_dbg(&pdev->dev, "power soft reset control not defined\n");
127 drv_data->pwr_rst = NULL;
128 }
129
130 return st_ahci_deassert_resets(&pdev->dev);
131}
132
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +0100133static struct ata_port_operations st_ahci_port_ops = {
134 .inherits = &ahci_platform_ops,
135 .host_stop = st_ahci_host_stop,
136};
137
Lee Jones76884cb2014-02-26 14:47:21 +0000138static const struct ata_port_info st_ahci_port_info = {
139 .flags = AHCI_FLAG_COMMON,
140 .pio_mask = ATA_PIO4,
141 .udma_mask = ATA_UDMA6,
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +0100142 .port_ops = &st_ahci_port_ops,
Lee Jones76884cb2014-02-26 14:47:21 +0000143};
144
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900145static struct scsi_host_template ahci_platform_sht = {
146 AHCI_SHT(DRV_NAME),
147};
148
Lee Jones76884cb2014-02-26 14:47:21 +0000149static int st_ahci_probe(struct platform_device *pdev)
150{
151 struct st_ahci_drv_data *drv_data;
152 struct ahci_host_priv *hpriv;
153 int err;
154
155 drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
156 if (!drv_data)
157 return -ENOMEM;
158
159 platform_set_drvdata(pdev, drv_data);
160
161 hpriv = ahci_platform_get_resources(pdev);
162 if (IS_ERR(hpriv))
163 return PTR_ERR(hpriv);
164
165 drv_data->hpriv = hpriv;
166
167 err = st_ahci_probe_resets(pdev);
168 if (err)
169 return err;
170
171 err = ahci_platform_enable_resources(hpriv);
172 if (err)
173 return err;
174
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900175 err = ahci_platform_init_host(pdev, hpriv, &st_ahci_port_info,
176 &ahci_platform_sht);
Lee Jones76884cb2014-02-26 14:47:21 +0000177 if (err) {
178 ahci_platform_disable_resources(hpriv);
179 return err;
180 }
181
182 return 0;
183}
184
Lee Jones76884cb2014-02-26 14:47:21 +0000185#ifdef CONFIG_PM_SLEEP
186static int st_ahci_suspend(struct device *dev)
187{
188 struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
189 struct ahci_host_priv *hpriv = drv_data->hpriv;
190 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{
211 struct st_ahci_drv_data *drv_data = dev_get_drvdata(dev);
212 struct ahci_host_priv *hpriv = drv_data->hpriv;
213 int err;
214
215 err = ahci_platform_enable_resources(hpriv);
216 if (err)
217 return err;
218
219 err = st_ahci_deassert_resets(dev);
220 if (err) {
221 ahci_platform_disable_resources(hpriv);
222 return err;
223 }
224
Lee Jones761a8c22014-03-12 12:39:42 +0000225 return ahci_platform_resume_host(dev);
Lee Jones76884cb2014-02-26 14:47:21 +0000226}
227#endif
228
229static SIMPLE_DEV_PM_OPS(st_ahci_pm_ops, st_ahci_suspend, st_ahci_resume);
230
Kiran Padwal09de99d2014-07-22 19:45:43 +0530231static const struct of_device_id st_ahci_match[] = {
Lee Jones76884cb2014-02-26 14:47:21 +0000232 { .compatible = "st,ahci", },
233 {},
234};
235MODULE_DEVICE_TABLE(of, st_ahci_match);
236
237static struct platform_driver st_ahci_driver = {
238 .driver = {
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900239 .name = DRV_NAME,
Lee Jones76884cb2014-02-26 14:47:21 +0000240 .pm = &st_ahci_pm_ops,
241 .of_match_table = of_match_ptr(st_ahci_match),
242 },
243 .probe = st_ahci_probe,
Lee Jonesa8237082014-03-12 12:39:40 +0000244 .remove = ata_platform_remove_one,
Lee Jones76884cb2014-02-26 14:47:21 +0000245};
246module_platform_driver(st_ahci_driver);
247
248MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
249MODULE_AUTHOR("Francesco Virlinzi <francesco.virlinzi@st.com>");
Lee Jones4a2e5122014-03-12 12:39:38 +0000250MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver");
Lee Jones76884cb2014-02-26 14:47:21 +0000251MODULE_LICENSE("GPL v2");