2 * Copyright (C) 2012 STMicroelectronics Limited
4 * Authors: Francesco Virlinzi <francesco.virlinzi@st.com>
5 * Alexandre Torgue <alexandre.torgue@st.com>
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.
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>
18 #include <linux/ahci_platform.h>
19 #include <linux/libata.h>
20 #include <linux/reset.h>
22 #include <linux/dma-mapping.h>
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
33 struct st_ahci_drv_data
{
34 struct platform_device
*ahci
;
35 struct reset_control
*pwr
;
36 struct reset_control
*sw_rst
;
37 struct reset_control
*pwr_rst
;
38 struct ahci_host_priv
*hpriv
;
41 static void st_ahci_configure_oob(void __iomem
*mmio
)
43 unsigned long old_val
, new_val
;
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
);
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
);
56 static int st_ahci_deassert_resets(struct device
*dev
)
58 struct st_ahci_drv_data
*drv_data
= dev_get_drvdata(dev
);
62 err
= reset_control_deassert(drv_data
->pwr
);
64 dev_err(dev
, "unable to bring out of pwrdwn\n");
69 st_ahci_configure_oob(drv_data
->hpriv
->mmio
);
71 if (drv_data
->sw_rst
) {
72 err
= reset_control_deassert(drv_data
->sw_rst
);
74 dev_err(dev
, "unable to bring out of sw-rst\n");
79 if (drv_data
->pwr_rst
) {
80 err
= reset_control_deassert(drv_data
->pwr_rst
);
82 dev_err(dev
, "unable to bring out of pwr-rst\n");
90 static int st_ahci_exit(struct device
*dev
)
92 struct st_ahci_drv_data
*drv_data
= dev_get_drvdata(dev
);
93 struct ahci_host_priv
*hpriv
= drv_data
->hpriv
;
97 err
= reset_control_assert(drv_data
->pwr
);
99 dev_err(&pdev
->dev
, "unable to pwrdwn\n");
102 ahci_platform_disable_resources(hpriv
);
107 static int st_ahci_probe_resets(struct platform_device
*pdev
)
109 struct st_ahci_drv_data
*drv_data
= platform_get_drvdata(pdev
);
111 drv_data
->pwr
= devm_reset_control_get(&pdev
->dev
, "pwr-dwn");
112 if (IS_ERR(drv_data
->pwr
)) {
113 dev_info(&pdev
->dev
, "power reset control not defined\n");
114 drv_data
->pwr
= NULL
;
117 drv_data
->sw_rst
= devm_reset_control_get(&pdev
->dev
, "sw-rst");
118 if (IS_ERR(drv_data
->sw_rst
)) {
119 dev_info(&pdev
->dev
, "soft reset control not defined\n");
120 drv_data
->sw_rst
= NULL
;
123 drv_data
->pwr_rst
= devm_reset_control_get(&pdev
->dev
, "pwr-rst");
124 if (IS_ERR(drv_data
->pwr_rst
)) {
125 dev_dbg(&pdev
->dev
, "power soft reset control not defined\n");
126 drv_data
->pwr_rst
= NULL
;
129 return st_ahci_deassert_resets(&pdev
->dev
);
132 static const struct ata_port_info st_ahci_port_info
= {
133 .flags
= AHCI_FLAG_COMMON
,
134 .pio_mask
= ATA_PIO4
,
135 .udma_mask
= ATA_UDMA6
,
136 .port_ops
= &ahci_platform_ops
,
139 static int st_ahci_probe(struct platform_device
*pdev
)
141 struct st_ahci_drv_data
*drv_data
;
142 struct ahci_platform_data
*pdata
;
143 struct ahci_host_priv
*hpriv
;
146 drv_data
= devm_kzalloc(&pdev
->dev
, sizeof(*drv_data
), GFP_KERNEL
);
150 platform_set_drvdata(pdev
, drv_data
);
152 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
156 pdata
->exit
= st_ahci_exit
;
157 pdev
->dev
.platform_data
= pdata
;
159 hpriv
= ahci_platform_get_resources(pdev
);
161 return PTR_ERR(hpriv
);
163 drv_data
->hpriv
= hpriv
;
165 err
= st_ahci_probe_resets(pdev
);
169 err
= ahci_platform_enable_resources(hpriv
);
173 err
= ahci_platform_init_host(pdev
, hpriv
, &st_ahci_port_info
, 0, 0);
175 ahci_platform_disable_resources(hpriv
);
182 #ifdef CONFIG_PM_SLEEP
183 static int st_ahci_suspend(struct device
*dev
)
185 struct st_ahci_drv_data
*drv_data
= dev_get_drvdata(dev
);
186 struct ahci_host_priv
*hpriv
= drv_data
->hpriv
;
190 err
= reset_control_assert(drv_data
->pwr
);
192 dev_err(dev
, "unable to pwrdwn");
197 ahci_platform_disable_resources(hpriv
);
202 static int st_ahci_resume(struct device
*dev
)
204 struct st_ahci_drv_data
*drv_data
= dev_get_drvdata(dev
);
205 struct ahci_host_priv
*hpriv
= drv_data
->hpriv
;
208 err
= ahci_platform_enable_resources(hpriv
);
212 err
= st_ahci_deassert_resets(dev
);
214 ahci_platform_disable_resources(hpriv
);
222 static SIMPLE_DEV_PM_OPS(st_ahci_pm_ops
, st_ahci_suspend
, st_ahci_resume
);
224 static struct of_device_id st_ahci_match
[] = {
225 { .compatible
= "st,ahci", },
228 MODULE_DEVICE_TABLE(of
, st_ahci_match
);
230 static struct platform_driver st_ahci_driver
= {
233 .owner
= THIS_MODULE
,
234 .pm
= &st_ahci_pm_ops
,
235 .of_match_table
= of_match_ptr(st_ahci_match
),
237 .probe
= st_ahci_probe
,
238 .remove
= ata_platform_remove_one
,
240 module_platform_driver(st_ahci_driver
);
242 MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
243 MODULE_AUTHOR("Francesco Virlinzi <francesco.virlinzi@st.com>");
244 MODULE_DESCRIPTION("STMicroelectronics SATA AHCI Driver");
245 MODULE_LICENSE("GPL v2");