blob: 8ff428fe8e0fa00659218f58f041cf948ae8327c [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;
Lee Jones76884cb2014-02-26 14:47:21 +000040};
41
42static void st_ahci_configure_oob(void __iomem *mmio)
43{
44 unsigned long old_val, new_val;
45
46 new_val = (0x02 << ST_AHCI_OOBR_CWMIN_SHIFT) |
47 (0x04 << ST_AHCI_OOBR_CWMAX_SHIFT) |
48 (0x08 << ST_AHCI_OOBR_CIMIN_SHIFT) |
49 (0x0C << ST_AHCI_OOBR_CIMAX_SHIFT);
50
51 old_val = readl(mmio + ST_AHCI_OOBR);
52 writel(old_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
53 writel(new_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
54 writel(new_val, mmio + ST_AHCI_OOBR);
55}
56
Peter Griffine0e26742015-04-20 14:41:05 +010057static int st_ahci_deassert_resets(struct ahci_host_priv *hpriv,
58 struct device *dev)
Lee Jones76884cb2014-02-26 14:47:21 +000059{
Peter Griffine0e26742015-04-20 14:41:05 +010060 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
Lee Jones76884cb2014-02-26 14:47:21 +000061 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
Lee Jones76884cb2014-02-26 14:47:21 +000071 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;
Peter Griffine0e26742015-04-20 14:41:05 +010093 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +010094 struct device *dev = host->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
Peter Griffine0e26742015-04-20 14:41:05 +0100106static int st_ahci_probe_resets(struct ahci_host_priv *hpriv,
107 struct device *dev)
Lee Jones76884cb2014-02-26 14:47:21 +0000108{
Peter Griffine0e26742015-04-20 14:41:05 +0100109 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
Lee Jones76884cb2014-02-26 14:47:21 +0000110
Peter Griffine0e26742015-04-20 14:41:05 +0100111 drv_data->pwr = devm_reset_control_get(dev, "pwr-dwn");
Lee Jones76884cb2014-02-26 14:47:21 +0000112 if (IS_ERR(drv_data->pwr)) {
Peter Griffine0e26742015-04-20 14:41:05 +0100113 dev_info(dev, "power reset control not defined\n");
Lee Jones76884cb2014-02-26 14:47:21 +0000114 drv_data->pwr = NULL;
115 }
116
Peter Griffine0e26742015-04-20 14:41:05 +0100117 drv_data->sw_rst = devm_reset_control_get(dev, "sw-rst");
Lee Jones76884cb2014-02-26 14:47:21 +0000118 if (IS_ERR(drv_data->sw_rst)) {
Peter Griffine0e26742015-04-20 14:41:05 +0100119 dev_info(dev, "soft reset control not defined\n");
Lee Jones76884cb2014-02-26 14:47:21 +0000120 drv_data->sw_rst = NULL;
121 }
122
Peter Griffine0e26742015-04-20 14:41:05 +0100123 drv_data->pwr_rst = devm_reset_control_get(dev, "pwr-rst");
Lee Jones76884cb2014-02-26 14:47:21 +0000124 if (IS_ERR(drv_data->pwr_rst)) {
Peter Griffine0e26742015-04-20 14:41:05 +0100125 dev_dbg(dev, "power soft reset control not defined\n");
Lee Jones76884cb2014-02-26 14:47:21 +0000126 drv_data->pwr_rst = NULL;
127 }
128
Peter Griffine0e26742015-04-20 14:41:05 +0100129 return st_ahci_deassert_resets(hpriv, dev);
Lee Jones76884cb2014-02-26 14:47:21 +0000130}
131
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +0100132static struct ata_port_operations st_ahci_port_ops = {
133 .inherits = &ahci_platform_ops,
134 .host_stop = st_ahci_host_stop,
135};
136
Lee Jones76884cb2014-02-26 14:47:21 +0000137static const struct ata_port_info st_ahci_port_info = {
138 .flags = AHCI_FLAG_COMMON,
139 .pio_mask = ATA_PIO4,
140 .udma_mask = ATA_UDMA6,
Bartlomiej Zolnierkiewiczb0323782014-03-14 19:21:59 +0100141 .port_ops = &st_ahci_port_ops,
Lee Jones76884cb2014-02-26 14:47:21 +0000142};
143
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900144static struct scsi_host_template ahci_platform_sht = {
145 AHCI_SHT(DRV_NAME),
146};
147
Lee Jones76884cb2014-02-26 14:47:21 +0000148static int st_ahci_probe(struct platform_device *pdev)
149{
150 struct st_ahci_drv_data *drv_data;
151 struct ahci_host_priv *hpriv;
152 int err;
153
154 drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
155 if (!drv_data)
156 return -ENOMEM;
157
Lee Jones76884cb2014-02-26 14:47:21 +0000158 hpriv = ahci_platform_get_resources(pdev);
159 if (IS_ERR(hpriv))
160 return PTR_ERR(hpriv);
Peter Griffine0e26742015-04-20 14:41:05 +0100161 hpriv->plat_data = drv_data;
Lee Jones76884cb2014-02-26 14:47:21 +0000162
Peter Griffine0e26742015-04-20 14:41:05 +0100163 err = st_ahci_probe_resets(hpriv, &pdev->dev);
Lee Jones76884cb2014-02-26 14:47:21 +0000164 if (err)
165 return err;
166
167 err = ahci_platform_enable_resources(hpriv);
168 if (err)
169 return err;
170
Peter Griffine0e26742015-04-20 14:41:05 +0100171 st_ahci_configure_oob(hpriv->mmio);
Peter Griffin9a1e75e2015-03-31 08:35:09 +0100172
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900173 err = ahci_platform_init_host(pdev, hpriv, &st_ahci_port_info,
174 &ahci_platform_sht);
Lee Jones76884cb2014-02-26 14:47:21 +0000175 if (err) {
176 ahci_platform_disable_resources(hpriv);
177 return err;
178 }
179
180 return 0;
181}
182
Lee Jones76884cb2014-02-26 14:47:21 +0000183#ifdef CONFIG_PM_SLEEP
184static int st_ahci_suspend(struct device *dev)
185{
Peter Griffine0e26742015-04-20 14:41:05 +0100186 struct ata_host *host = dev_get_drvdata(dev);
187 struct ahci_host_priv *hpriv = host->private_data;
188 struct st_ahci_drv_data *drv_data = hpriv->plat_data;
Lee Jones76884cb2014-02-26 14:47:21 +0000189 int err;
190
Bartlomiej Zolnierkiewicz33081b32014-03-14 19:20:58 +0100191 err = ahci_platform_suspend_host(dev);
192 if (err)
193 return err;
Lee Jones761a8c22014-03-12 12:39:42 +0000194
Lee Jones76884cb2014-02-26 14:47:21 +0000195 if (drv_data->pwr) {
196 err = reset_control_assert(drv_data->pwr);
197 if (err) {
198 dev_err(dev, "unable to pwrdwn");
199 return err;
200 }
201 }
202
203 ahci_platform_disable_resources(hpriv);
204
205 return 0;
206}
207
208static int st_ahci_resume(struct device *dev)
209{
Peter Griffine0e26742015-04-20 14:41:05 +0100210 struct ata_host *host = dev_get_drvdata(dev);
211 struct ahci_host_priv *hpriv = host->private_data;
Lee Jones76884cb2014-02-26 14:47:21 +0000212 int err;
213
214 err = ahci_platform_enable_resources(hpriv);
215 if (err)
216 return err;
217
Peter Griffine0e26742015-04-20 14:41:05 +0100218 err = st_ahci_deassert_resets(hpriv, dev);
Lee Jones76884cb2014-02-26 14:47:21 +0000219 if (err) {
220 ahci_platform_disable_resources(hpriv);
221 return err;
222 }
223
Peter Griffine0e26742015-04-20 14:41:05 +0100224 st_ahci_configure_oob(hpriv->mmio);
Peter Griffin9a1e75e2015-03-31 08:35:09 +0100225
Lee Jones761a8c22014-03-12 12:39:42 +0000226 return ahci_platform_resume_host(dev);
Lee Jones76884cb2014-02-26 14:47:21 +0000227}
228#endif
229
230static SIMPLE_DEV_PM_OPS(st_ahci_pm_ops, st_ahci_suspend, st_ahci_resume);
231
Kiran Padwal09de99d2014-07-22 19:45:43 +0530232static const struct of_device_id st_ahci_match[] = {
Lee Jones76884cb2014-02-26 14:47:21 +0000233 { .compatible = "st,ahci", },
234 {},
235};
236MODULE_DEVICE_TABLE(of, st_ahci_match);
237
238static struct platform_driver st_ahci_driver = {
239 .driver = {
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900240 .name = DRV_NAME,
Lee Jones76884cb2014-02-26 14:47:21 +0000241 .pm = &st_ahci_pm_ops,
242 .of_match_table = of_match_ptr(st_ahci_match),
243 },
244 .probe = st_ahci_probe,
Lee Jonesa8237082014-03-12 12:39:40 +0000245 .remove = ata_platform_remove_one,
Lee Jones76884cb2014-02-26 14:47:21 +0000246};
247module_platform_driver(st_ahci_driver);
248
249MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
250MODULE_AUTHOR("Francesco Virlinzi <francesco.virlinzi@st.com>");
Lee Jones4a2e5122014-03-12 12:39:38 +0000251MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver");
Lee Jones76884cb2014-02-26 14:47:21 +0000252MODULE_LICENSE("GPL v2");