Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2012 STMicroelectronics Limited
  4 *
  5 * Authors: Francesco Virlinzi <francesco.virlinzi@st.com>
  6 *	    Alexandre Torgue <alexandre.torgue@st.com>
 
 
 
 
  7 */
  8
  9#include <linux/init.h>
 10#include <linux/module.h>
 11#include <linux/export.h>
 12#include <linux/platform_device.h>
 13#include <linux/clk.h>
 14#include <linux/of.h>
 15#include <linux/ahci_platform.h>
 16#include <linux/libata.h>
 17#include <linux/reset.h>
 18#include <linux/io.h>
 19#include <linux/dma-mapping.h>
 20
 21#include "ahci.h"
 22
 23#define DRV_NAME  "st_ahci"
 24
 25#define ST_AHCI_OOBR			0xbc
 26#define ST_AHCI_OOBR_WE			BIT(31)
 27#define ST_AHCI_OOBR_CWMIN_SHIFT	24
 28#define ST_AHCI_OOBR_CWMAX_SHIFT	16
 29#define ST_AHCI_OOBR_CIMIN_SHIFT	8
 30#define ST_AHCI_OOBR_CIMAX_SHIFT	0
 31
 32struct st_ahci_drv_data {
 33	struct platform_device *ahci;
 34	struct reset_control *pwr;
 35	struct reset_control *sw_rst;
 36	struct reset_control *pwr_rst;
 37};
 38
 39static void st_ahci_configure_oob(void __iomem *mmio)
 40{
 41	unsigned long old_val, new_val;
 42
 43	new_val = (0x02 << ST_AHCI_OOBR_CWMIN_SHIFT) |
 44		  (0x04 << ST_AHCI_OOBR_CWMAX_SHIFT) |
 45		  (0x08 << ST_AHCI_OOBR_CIMIN_SHIFT) |
 46		  (0x0C << ST_AHCI_OOBR_CIMAX_SHIFT);
 47
 48	old_val = readl(mmio + ST_AHCI_OOBR);
 49	writel(old_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
 50	writel(new_val | ST_AHCI_OOBR_WE, mmio + ST_AHCI_OOBR);
 51	writel(new_val, mmio + ST_AHCI_OOBR);
 52}
 53
 54static int st_ahci_deassert_resets(struct ahci_host_priv *hpriv,
 55				struct device *dev)
 56{
 57	struct st_ahci_drv_data *drv_data = hpriv->plat_data;
 58	int err;
 59
 60	if (drv_data->pwr) {
 61		err = reset_control_deassert(drv_data->pwr);
 62		if (err) {
 63			dev_err(dev, "unable to bring out of pwrdwn\n");
 64			return err;
 65		}
 66	}
 67
 68	if (drv_data->sw_rst) {
 69		err = reset_control_deassert(drv_data->sw_rst);
 70		if (err) {
 71			dev_err(dev, "unable to bring out of sw-rst\n");
 72			return err;
 73		}
 74	}
 75
 76	if (drv_data->pwr_rst) {
 77		err = reset_control_deassert(drv_data->pwr_rst);
 78		if (err) {
 79			dev_err(dev, "unable to bring out of pwr-rst\n");
 80			return err;
 81		}
 82	}
 83
 84	return 0;
 85}
 86
 87static void st_ahci_host_stop(struct ata_host *host)
 88{
 89	struct ahci_host_priv *hpriv = host->private_data;
 90	struct st_ahci_drv_data *drv_data = hpriv->plat_data;
 91	struct device *dev = host->dev;
 92	int err;
 93
 94	if (drv_data->pwr) {
 95		err = reset_control_assert(drv_data->pwr);
 96		if (err)
 97			dev_err(dev, "unable to pwrdwn\n");
 98	}
 99
100	ahci_platform_disable_resources(hpriv);
101}
102
103static int st_ahci_probe_resets(struct ahci_host_priv *hpriv,
104				struct device *dev)
105{
106	struct st_ahci_drv_data *drv_data = hpriv->plat_data;
107
108	drv_data->pwr = devm_reset_control_get(dev, "pwr-dwn");
109	if (IS_ERR(drv_data->pwr)) {
110		dev_info(dev, "power reset control not defined\n");
111		drv_data->pwr = NULL;
112	}
113
114	drv_data->sw_rst = devm_reset_control_get(dev, "sw-rst");
115	if (IS_ERR(drv_data->sw_rst)) {
116		dev_info(dev, "soft reset control not defined\n");
117		drv_data->sw_rst = NULL;
118	}
119
120	drv_data->pwr_rst = devm_reset_control_get(dev, "pwr-rst");
121	if (IS_ERR(drv_data->pwr_rst)) {
122		dev_dbg(dev, "power soft reset control not defined\n");
123		drv_data->pwr_rst = NULL;
124	}
125
126	return st_ahci_deassert_resets(hpriv, dev);
127}
128
129static struct ata_port_operations st_ahci_port_ops = {
130	.inherits	= &ahci_platform_ops,
131	.host_stop	= st_ahci_host_stop,
132};
133
134static const struct ata_port_info st_ahci_port_info = {
135	.flags          = AHCI_FLAG_COMMON,
136	.pio_mask       = ATA_PIO4,
137	.udma_mask      = ATA_UDMA6,
138	.port_ops       = &st_ahci_port_ops,
139};
140
141static const struct scsi_host_template ahci_platform_sht = {
142	AHCI_SHT(DRV_NAME),
143};
144
145static int st_ahci_probe(struct platform_device *pdev)
146{
147	struct st_ahci_drv_data *drv_data;
148	struct ahci_host_priv *hpriv;
149	int err;
150
151	drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
152	if (!drv_data)
153		return -ENOMEM;
154
155	hpriv = ahci_platform_get_resources(pdev, 0);
156	if (IS_ERR(hpriv))
157		return PTR_ERR(hpriv);
158	hpriv->plat_data = drv_data;
159
160	err = st_ahci_probe_resets(hpriv, &pdev->dev);
161	if (err)
162		return err;
163
164	err = ahci_platform_enable_resources(hpriv);
165	if (err)
166		return err;
167
168	st_ahci_configure_oob(hpriv->mmio);
169
170	err = ahci_platform_init_host(pdev, hpriv, &st_ahci_port_info,
171				      &ahci_platform_sht);
172	if (err) {
173		ahci_platform_disable_resources(hpriv);
174		return err;
175	}
176
177	return 0;
178}
179
180#ifdef CONFIG_PM_SLEEP
181static int st_ahci_suspend(struct device *dev)
182{
183	struct ata_host *host = dev_get_drvdata(dev);
184	struct ahci_host_priv *hpriv = host->private_data;
185	struct st_ahci_drv_data *drv_data = hpriv->plat_data;
186	int err;
187
188	err = ahci_platform_suspend_host(dev);
189	if (err)
190		return err;
191
192	if (drv_data->pwr) {
193		err = reset_control_assert(drv_data->pwr);
194		if (err) {
195			dev_err(dev, "unable to pwrdwn");
196			return err;
197		}
198	}
199
200	ahci_platform_disable_resources(hpriv);
201
202	return 0;
203}
204
205static int st_ahci_resume(struct device *dev)
206{
207	struct ata_host *host = dev_get_drvdata(dev);
208	struct ahci_host_priv *hpriv = host->private_data;
209	int err;
210
211	err = ahci_platform_enable_resources(hpriv);
212	if (err)
213		return err;
214
215	err = st_ahci_deassert_resets(hpriv, dev);
216	if (err) {
217		ahci_platform_disable_resources(hpriv);
218		return err;
219	}
220
221	st_ahci_configure_oob(hpriv->mmio);
222
223	return ahci_platform_resume_host(dev);
224}
225#endif
226
227static SIMPLE_DEV_PM_OPS(st_ahci_pm_ops, st_ahci_suspend, st_ahci_resume);
228
229static const struct of_device_id st_ahci_match[] = {
230	{ .compatible = "st,ahci", },
231	{ /* sentinel */ }
232};
233MODULE_DEVICE_TABLE(of, st_ahci_match);
234
235static struct platform_driver st_ahci_driver = {
236	.driver = {
237		.name = DRV_NAME,
238		.pm = &st_ahci_pm_ops,
239		.of_match_table = st_ahci_match,
240	},
241	.probe = st_ahci_probe,
242	.remove_new = ata_platform_remove_one,
243};
244module_platform_driver(st_ahci_driver);
245
246MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
247MODULE_AUTHOR("Francesco Virlinzi <francesco.virlinzi@st.com>");
248MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver");
249MODULE_LICENSE("GPL v2");
v4.6
 
  1/*
  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>
 19#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 DRV_NAME  "st_ahci"
 27
 28#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;
 37	struct reset_control *pwr;
 38	struct reset_control *sw_rst;
 39	struct reset_control *pwr_rst;
 40};
 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
 57static int st_ahci_deassert_resets(struct ahci_host_priv *hpriv,
 58				struct device *dev)
 59{
 60	struct st_ahci_drv_data *drv_data = hpriv->plat_data;
 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	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 void st_ahci_host_stop(struct ata_host *host)
 91{
 92	struct ahci_host_priv *hpriv = host->private_data;
 93	struct st_ahci_drv_data *drv_data = hpriv->plat_data;
 94	struct device *dev = host->dev;
 95	int err;
 96
 97	if (drv_data->pwr) {
 98		err = reset_control_assert(drv_data->pwr);
 99		if (err)
100			dev_err(dev, "unable to pwrdwn\n");
101	}
102
103	ahci_platform_disable_resources(hpriv);
104}
105
106static int st_ahci_probe_resets(struct ahci_host_priv *hpriv,
107				struct device *dev)
108{
109	struct st_ahci_drv_data *drv_data = hpriv->plat_data;
110
111	drv_data->pwr = devm_reset_control_get(dev, "pwr-dwn");
112	if (IS_ERR(drv_data->pwr)) {
113		dev_info(dev, "power reset control not defined\n");
114		drv_data->pwr = NULL;
115	}
116
117	drv_data->sw_rst = devm_reset_control_get(dev, "sw-rst");
118	if (IS_ERR(drv_data->sw_rst)) {
119		dev_info(dev, "soft reset control not defined\n");
120		drv_data->sw_rst = NULL;
121	}
122
123	drv_data->pwr_rst = devm_reset_control_get(dev, "pwr-rst");
124	if (IS_ERR(drv_data->pwr_rst)) {
125		dev_dbg(dev, "power soft reset control not defined\n");
126		drv_data->pwr_rst = NULL;
127	}
128
129	return st_ahci_deassert_resets(hpriv, dev);
130}
131
132static struct ata_port_operations st_ahci_port_ops = {
133	.inherits	= &ahci_platform_ops,
134	.host_stop	= st_ahci_host_stop,
135};
136
137static const struct ata_port_info st_ahci_port_info = {
138	.flags          = AHCI_FLAG_COMMON,
139	.pio_mask       = ATA_PIO4,
140	.udma_mask      = ATA_UDMA6,
141	.port_ops       = &st_ahci_port_ops,
142};
143
144static struct scsi_host_template ahci_platform_sht = {
145	AHCI_SHT(DRV_NAME),
146};
147
148static 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
158	hpriv = ahci_platform_get_resources(pdev);
159	if (IS_ERR(hpriv))
160		return PTR_ERR(hpriv);
161	hpriv->plat_data = drv_data;
162
163	err = st_ahci_probe_resets(hpriv, &pdev->dev);
164	if (err)
165		return err;
166
167	err = ahci_platform_enable_resources(hpriv);
168	if (err)
169		return err;
170
171	st_ahci_configure_oob(hpriv->mmio);
172
173	err = ahci_platform_init_host(pdev, hpriv, &st_ahci_port_info,
174				      &ahci_platform_sht);
175	if (err) {
176		ahci_platform_disable_resources(hpriv);
177		return err;
178	}
179
180	return 0;
181}
182
183#ifdef CONFIG_PM_SLEEP
184static int st_ahci_suspend(struct device *dev)
185{
186	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;
189	int err;
190
191	err = ahci_platform_suspend_host(dev);
192	if (err)
193		return err;
194
195	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{
210	struct ata_host *host = dev_get_drvdata(dev);
211	struct ahci_host_priv *hpriv = host->private_data;
212	int err;
213
214	err = ahci_platform_enable_resources(hpriv);
215	if (err)
216		return err;
217
218	err = st_ahci_deassert_resets(hpriv, dev);
219	if (err) {
220		ahci_platform_disable_resources(hpriv);
221		return err;
222	}
223
224	st_ahci_configure_oob(hpriv->mmio);
225
226	return ahci_platform_resume_host(dev);
227}
228#endif
229
230static SIMPLE_DEV_PM_OPS(st_ahci_pm_ops, st_ahci_suspend, st_ahci_resume);
231
232static const struct of_device_id st_ahci_match[] = {
233	{ .compatible = "st,ahci", },
234	{},
235};
236MODULE_DEVICE_TABLE(of, st_ahci_match);
237
238static struct platform_driver st_ahci_driver = {
239	.driver = {
240		.name = DRV_NAME,
241		.pm = &st_ahci_pm_ops,
242		.of_match_table = of_match_ptr(st_ahci_match),
243	},
244	.probe = st_ahci_probe,
245	.remove = ata_platform_remove_one,
246};
247module_platform_driver(st_ahci_driver);
248
249MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
250MODULE_AUTHOR("Francesco Virlinzi <francesco.virlinzi@st.com>");
251MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver");
252MODULE_LICENSE("GPL v2");