blob: 256d9114a1941f7b3051fc69405bd524dc7b24e3 [file] [log] [blame]
Tang Yuantianecfb4592015-09-07 16:23:16 +08001/*
2 * Freescale QorIQ AHCI SATA platform driver
3 *
4 * Copyright 2015 Freescale, Inc.
5 * Tang Yuantian <Yuantian.Tang@freescale.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 as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/pm.h>
16#include <linux/ahci_platform.h>
17#include <linux/device.h>
18#include <linux/of_address.h>
19#include <linux/of.h>
20#include <linux/of_device.h>
21#include <linux/platform_device.h>
22#include <linux/libata.h>
23#include "ahci.h"
24
25#define DRV_NAME "ahci-qoriq"
26
27/* port register definition */
28#define PORT_PHY1 0xA8
29#define PORT_PHY2 0xAC
30#define PORT_PHY3 0xB0
31#define PORT_PHY4 0xB4
32#define PORT_PHY5 0xB8
33#define PORT_TRANS 0xC8
34
35/* port register default value */
36#define AHCI_PORT_PHY_1_CFG 0xa003fffe
37#define AHCI_PORT_PHY_2_CFG 0x28183411
38#define AHCI_PORT_PHY_3_CFG 0x0e081004
39#define AHCI_PORT_PHY_4_CFG 0x00480811
40#define AHCI_PORT_PHY_5_CFG 0x192c96a4
Tang Yuantiane3a6dad2015-12-16 13:43:50 +080041#define AHCI_PORT_TRANS_CFG 0x08000029
Tang Yuantianef0cc7f2015-12-16 13:43:49 +080042#define LS1043A_PORT_PHY2 0x28184d1f
43#define LS1043A_PORT_PHY3 0x0e081509
Tang Yuantianecfb4592015-09-07 16:23:16 +080044
45#define SATA_ECC_DISABLE 0x00020000
46
47enum ahci_qoriq_type {
48 AHCI_LS1021A,
49 AHCI_LS1043A,
Tang Yuantiand19f9aa2015-10-29 14:22:15 +080050 AHCI_LS2080A,
Tang Yuantianecfb4592015-09-07 16:23:16 +080051};
52
53struct ahci_qoriq_priv {
54 struct ccsr_ahci *reg_base;
55 enum ahci_qoriq_type type;
56 void __iomem *ecc_addr;
57};
58
59static const struct of_device_id ahci_qoriq_of_match[] = {
60 { .compatible = "fsl,ls1021a-ahci", .data = (void *)AHCI_LS1021A},
61 { .compatible = "fsl,ls1043a-ahci", .data = (void *)AHCI_LS1043A},
Tang Yuantiand19f9aa2015-10-29 14:22:15 +080062 { .compatible = "fsl,ls2080a-ahci", .data = (void *)AHCI_LS2080A},
Tang Yuantianecfb4592015-09-07 16:23:16 +080063 {},
64};
65MODULE_DEVICE_TABLE(of, ahci_qoriq_of_match);
66
67static int ahci_qoriq_hardreset(struct ata_link *link, unsigned int *class,
68 unsigned long deadline)
69{
70 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
71 void __iomem *port_mmio = ahci_port_base(link->ap);
72 u32 px_cmd, px_is, px_val;
73 struct ata_port *ap = link->ap;
74 struct ahci_port_priv *pp = ap->private_data;
75 struct ahci_host_priv *hpriv = ap->host->private_data;
76 struct ahci_qoriq_priv *qoriq_priv = hpriv->plat_data;
77 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
78 struct ata_taskfile tf;
79 bool online;
80 int rc;
Arnd Bergmanneb351032015-10-14 16:46:52 +080081 bool ls1021a_workaround = (qoriq_priv->type == AHCI_LS1021A);
Tang Yuantianecfb4592015-09-07 16:23:16 +080082
83 DPRINTK("ENTER\n");
84
85 ahci_stop_engine(ap);
86
87 /*
88 * There is a errata on ls1021a Rev1.0 and Rev2.0 which is:
89 * A-009042: The device detection initialization sequence
90 * mistakenly resets some registers.
91 *
92 * Workaround for this is:
93 * The software should read and store PxCMD and PxIS values
94 * before issuing the device detection initialization sequence.
95 * After the sequence is complete, software should restore the
96 * PxCMD and PxIS with the stored values.
97 */
Arnd Bergmanneb351032015-10-14 16:46:52 +080098 if (ls1021a_workaround) {
Tang Yuantianecfb4592015-09-07 16:23:16 +080099 px_cmd = readl(port_mmio + PORT_CMD);
100 px_is = readl(port_mmio + PORT_IRQ_STAT);
101 }
102
103 /* clear D2H reception area to properly wait for D2H FIS */
104 ata_tf_init(link->device, &tf);
105 tf.command = ATA_BUSY;
106 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
107
108 rc = sata_link_hardreset(link, timing, deadline, &online,
109 ahci_check_ready);
110
111 /* restore the PxCMD and PxIS on ls1021 */
Arnd Bergmanneb351032015-10-14 16:46:52 +0800112 if (ls1021a_workaround) {
Tang Yuantianecfb4592015-09-07 16:23:16 +0800113 px_val = readl(port_mmio + PORT_CMD);
114 if (px_val != px_cmd)
115 writel(px_cmd, port_mmio + PORT_CMD);
116
117 px_val = readl(port_mmio + PORT_IRQ_STAT);
118 if (px_val != px_is)
119 writel(px_is, port_mmio + PORT_IRQ_STAT);
120 }
121
122 hpriv->start_engine(ap);
123
124 if (online)
125 *class = ahci_dev_classify(ap);
126
127 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
128 return rc;
129}
130
131static struct ata_port_operations ahci_qoriq_ops = {
132 .inherits = &ahci_ops,
133 .hardreset = ahci_qoriq_hardreset,
134};
135
Tang Yuantian64084722015-10-29 14:22:16 +0800136static struct ata_port_info ahci_qoriq_port_info = {
Tang Yuantianecfb4592015-09-07 16:23:16 +0800137 .flags = AHCI_FLAG_COMMON | ATA_FLAG_NCQ,
138 .pio_mask = ATA_PIO4,
139 .udma_mask = ATA_UDMA6,
140 .port_ops = &ahci_qoriq_ops,
141};
142
143static struct scsi_host_template ahci_qoriq_sht = {
144 AHCI_SHT(DRV_NAME),
145};
146
147static int ahci_qoriq_phy_init(struct ahci_host_priv *hpriv)
148{
149 struct ahci_qoriq_priv *qpriv = hpriv->plat_data;
150 void __iomem *reg_base = hpriv->mmio;
151
152 switch (qpriv->type) {
153 case AHCI_LS1021A:
154 writel(SATA_ECC_DISABLE, qpriv->ecc_addr);
155 writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
156 writel(AHCI_PORT_PHY_2_CFG, reg_base + PORT_PHY2);
157 writel(AHCI_PORT_PHY_3_CFG, reg_base + PORT_PHY3);
158 writel(AHCI_PORT_PHY_4_CFG, reg_base + PORT_PHY4);
159 writel(AHCI_PORT_PHY_5_CFG, reg_base + PORT_PHY5);
160 writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
161 break;
162
163 case AHCI_LS1043A:
Tang Yuantianef0cc7f2015-12-16 13:43:49 +0800164 writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
165 writel(LS1043A_PORT_PHY2, reg_base + PORT_PHY2);
166 writel(LS1043A_PORT_PHY3, reg_base + PORT_PHY3);
167 writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
168 break;
169
Tang Yuantiand19f9aa2015-10-29 14:22:15 +0800170 case AHCI_LS2080A:
Tang Yuantianecfb4592015-09-07 16:23:16 +0800171 writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
Tang Yuantiane3a6dad2015-12-16 13:43:50 +0800172 writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
Tang Yuantianecfb4592015-09-07 16:23:16 +0800173 break;
174 }
175
176 return 0;
177}
178
179static int ahci_qoriq_probe(struct platform_device *pdev)
180{
181 struct device_node *np = pdev->dev.of_node;
182 struct device *dev = &pdev->dev;
183 struct ahci_host_priv *hpriv;
184 struct ahci_qoriq_priv *qoriq_priv;
185 const struct of_device_id *of_id;
186 struct resource *res;
187 int rc;
188
189 hpriv = ahci_platform_get_resources(pdev);
190 if (IS_ERR(hpriv))
191 return PTR_ERR(hpriv);
192
193 of_id = of_match_node(ahci_qoriq_of_match, np);
194 if (!of_id)
195 return -ENODEV;
196
197 qoriq_priv = devm_kzalloc(dev, sizeof(*qoriq_priv), GFP_KERNEL);
198 if (!qoriq_priv)
199 return -ENOMEM;
200
201 qoriq_priv->type = (enum ahci_qoriq_type)of_id->data;
202
203 if (qoriq_priv->type == AHCI_LS1021A) {
204 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
205 "sata-ecc");
206 qoriq_priv->ecc_addr = devm_ioremap_resource(dev, res);
207 if (IS_ERR(qoriq_priv->ecc_addr))
208 return PTR_ERR(qoriq_priv->ecc_addr);
209 }
210
211 rc = ahci_platform_enable_resources(hpriv);
212 if (rc)
213 return rc;
214
215 hpriv->plat_data = qoriq_priv;
216 rc = ahci_qoriq_phy_init(hpriv);
217 if (rc)
218 goto disable_resources;
219
Tang Yuantian64084722015-10-29 14:22:16 +0800220 /* Workaround for ls2080a */
221 if (qoriq_priv->type == AHCI_LS2080A) {
222 hpriv->flags |= AHCI_HFLAG_NO_NCQ;
223 ahci_qoriq_port_info.flags &= ~ATA_FLAG_NCQ;
224 }
225
Tang Yuantianecfb4592015-09-07 16:23:16 +0800226 rc = ahci_platform_init_host(pdev, hpriv, &ahci_qoriq_port_info,
227 &ahci_qoriq_sht);
228 if (rc)
229 goto disable_resources;
230
231 return 0;
232
233disable_resources:
234 ahci_platform_disable_resources(hpriv);
235
236 return rc;
237}
238
239#ifdef CONFIG_PM_SLEEP
240static int ahci_qoriq_resume(struct device *dev)
241{
242 struct ata_host *host = dev_get_drvdata(dev);
243 struct ahci_host_priv *hpriv = host->private_data;
244 int rc;
245
246 rc = ahci_platform_enable_resources(hpriv);
247 if (rc)
248 return rc;
249
250 rc = ahci_qoriq_phy_init(hpriv);
251 if (rc)
252 goto disable_resources;
253
254 rc = ahci_platform_resume_host(dev);
255 if (rc)
256 goto disable_resources;
257
258 /* We resumed so update PM runtime state */
259 pm_runtime_disable(dev);
260 pm_runtime_set_active(dev);
261 pm_runtime_enable(dev);
262
263 return 0;
264
265disable_resources:
266 ahci_platform_disable_resources(hpriv);
267
268 return rc;
269}
270#endif
271
272static SIMPLE_DEV_PM_OPS(ahci_qoriq_pm_ops, ahci_platform_suspend,
273 ahci_qoriq_resume);
274
275static struct platform_driver ahci_qoriq_driver = {
276 .probe = ahci_qoriq_probe,
277 .remove = ata_platform_remove_one,
278 .driver = {
279 .name = DRV_NAME,
280 .of_match_table = ahci_qoriq_of_match,
281 .pm = &ahci_qoriq_pm_ops,
282 },
283};
284module_platform_driver(ahci_qoriq_driver);
285
286MODULE_DESCRIPTION("Freescale QorIQ AHCI SATA platform driver");
287MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
288MODULE_LICENSE("GPL");