blob: 85d833289f28f85de9aa98efe52a05a921cdc3bd [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
Tang Yuantian16af0802016-08-09 09:51:22 +080033#define PORT_AXICC 0xBC
Tang Yuantianecfb4592015-09-07 16:23:16 +080034#define PORT_TRANS 0xC8
35
36/* port register default value */
37#define AHCI_PORT_PHY_1_CFG 0xa003fffe
Tang Yuantiane3a6dad2015-12-16 13:43:50 +080038#define AHCI_PORT_TRANS_CFG 0x08000029
Tang Yuantian16af0802016-08-09 09:51:22 +080039#define AHCI_PORT_AXICC_CFG 0x3fffffff
Tang Yuantiandfcdc5f2015-12-16 14:00:35 +080040
41/* for ls1021a */
42#define LS1021A_PORT_PHY2 0x28183414
43#define LS1021A_PORT_PHY3 0x0e080e06
44#define LS1021A_PORT_PHY4 0x064a080b
45#define LS1021A_PORT_PHY5 0x2aa86470
Tang Yuantian16af0802016-08-09 09:51:22 +080046#define LS1021A_AXICC_ADDR 0xC0
Tang Yuantianecfb4592015-09-07 16:23:16 +080047
48#define SATA_ECC_DISABLE 0x00020000
Tang Yuantian01f29012017-01-20 14:59:35 +080049#define ECC_DIS_ARMV8_CH2 0x80000000
Tang Yuantianecfb4592015-09-07 16:23:16 +080050
51enum ahci_qoriq_type {
52 AHCI_LS1021A,
53 AHCI_LS1043A,
Tang Yuantiand19f9aa2015-10-29 14:22:15 +080054 AHCI_LS2080A,
Tang Yuantian2facc6d2016-10-09 16:51:04 +080055 AHCI_LS1046A,
Tang Yuantiance8f4532017-01-20 14:59:36 +080056 AHCI_LS2088A,
Tang Yuantianecfb4592015-09-07 16:23:16 +080057};
58
59struct ahci_qoriq_priv {
60 struct ccsr_ahci *reg_base;
61 enum ahci_qoriq_type type;
62 void __iomem *ecc_addr;
Tang Yuantian386dc3b2017-01-20 14:59:34 +080063 bool is_dmacoherent;
Tang Yuantianecfb4592015-09-07 16:23:16 +080064};
65
66static const struct of_device_id ahci_qoriq_of_match[] = {
67 { .compatible = "fsl,ls1021a-ahci", .data = (void *)AHCI_LS1021A},
68 { .compatible = "fsl,ls1043a-ahci", .data = (void *)AHCI_LS1043A},
Tang Yuantiand19f9aa2015-10-29 14:22:15 +080069 { .compatible = "fsl,ls2080a-ahci", .data = (void *)AHCI_LS2080A},
Tang Yuantian2facc6d2016-10-09 16:51:04 +080070 { .compatible = "fsl,ls1046a-ahci", .data = (void *)AHCI_LS1046A},
Tang Yuantiance8f4532017-01-20 14:59:36 +080071 { .compatible = "fsl,ls2088a-ahci", .data = (void *)AHCI_LS2088A},
Tang Yuantianecfb4592015-09-07 16:23:16 +080072 {},
73};
74MODULE_DEVICE_TABLE(of, ahci_qoriq_of_match);
75
76static int ahci_qoriq_hardreset(struct ata_link *link, unsigned int *class,
77 unsigned long deadline)
78{
79 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
80 void __iomem *port_mmio = ahci_port_base(link->ap);
81 u32 px_cmd, px_is, px_val;
82 struct ata_port *ap = link->ap;
83 struct ahci_port_priv *pp = ap->private_data;
84 struct ahci_host_priv *hpriv = ap->host->private_data;
85 struct ahci_qoriq_priv *qoriq_priv = hpriv->plat_data;
86 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
87 struct ata_taskfile tf;
88 bool online;
89 int rc;
Arnd Bergmanneb351032015-10-14 16:46:52 +080090 bool ls1021a_workaround = (qoriq_priv->type == AHCI_LS1021A);
Tang Yuantianecfb4592015-09-07 16:23:16 +080091
92 DPRINTK("ENTER\n");
93
94 ahci_stop_engine(ap);
95
96 /*
97 * There is a errata on ls1021a Rev1.0 and Rev2.0 which is:
98 * A-009042: The device detection initialization sequence
99 * mistakenly resets some registers.
100 *
101 * Workaround for this is:
102 * The software should read and store PxCMD and PxIS values
103 * before issuing the device detection initialization sequence.
104 * After the sequence is complete, software should restore the
105 * PxCMD and PxIS with the stored values.
106 */
Arnd Bergmanneb351032015-10-14 16:46:52 +0800107 if (ls1021a_workaround) {
Tang Yuantianecfb4592015-09-07 16:23:16 +0800108 px_cmd = readl(port_mmio + PORT_CMD);
109 px_is = readl(port_mmio + PORT_IRQ_STAT);
110 }
111
112 /* clear D2H reception area to properly wait for D2H FIS */
113 ata_tf_init(link->device, &tf);
114 tf.command = ATA_BUSY;
115 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
116
117 rc = sata_link_hardreset(link, timing, deadline, &online,
118 ahci_check_ready);
119
120 /* restore the PxCMD and PxIS on ls1021 */
Arnd Bergmanneb351032015-10-14 16:46:52 +0800121 if (ls1021a_workaround) {
Tang Yuantianecfb4592015-09-07 16:23:16 +0800122 px_val = readl(port_mmio + PORT_CMD);
123 if (px_val != px_cmd)
124 writel(px_cmd, port_mmio + PORT_CMD);
125
126 px_val = readl(port_mmio + PORT_IRQ_STAT);
127 if (px_val != px_is)
128 writel(px_is, port_mmio + PORT_IRQ_STAT);
129 }
130
131 hpriv->start_engine(ap);
132
133 if (online)
134 *class = ahci_dev_classify(ap);
135
136 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
137 return rc;
138}
139
140static struct ata_port_operations ahci_qoriq_ops = {
141 .inherits = &ahci_ops,
142 .hardreset = ahci_qoriq_hardreset,
143};
144
Tang Yuantian1ce788d2016-09-30 14:13:13 +0800145static const struct ata_port_info ahci_qoriq_port_info = {
Tang Yuantianecfb4592015-09-07 16:23:16 +0800146 .flags = AHCI_FLAG_COMMON | ATA_FLAG_NCQ,
147 .pio_mask = ATA_PIO4,
148 .udma_mask = ATA_UDMA6,
149 .port_ops = &ahci_qoriq_ops,
150};
151
152static struct scsi_host_template ahci_qoriq_sht = {
153 AHCI_SHT(DRV_NAME),
154};
155
156static int ahci_qoriq_phy_init(struct ahci_host_priv *hpriv)
157{
158 struct ahci_qoriq_priv *qpriv = hpriv->plat_data;
159 void __iomem *reg_base = hpriv->mmio;
160
161 switch (qpriv->type) {
162 case AHCI_LS1021A:
Tang Yuantian01f29012017-01-20 14:59:35 +0800163 if (!qpriv->ecc_addr)
164 return -EINVAL;
Tang Yuantianecfb4592015-09-07 16:23:16 +0800165 writel(SATA_ECC_DISABLE, qpriv->ecc_addr);
166 writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
Tang Yuantiandfcdc5f2015-12-16 14:00:35 +0800167 writel(LS1021A_PORT_PHY2, reg_base + PORT_PHY2);
168 writel(LS1021A_PORT_PHY3, reg_base + PORT_PHY3);
169 writel(LS1021A_PORT_PHY4, reg_base + PORT_PHY4);
170 writel(LS1021A_PORT_PHY5, reg_base + PORT_PHY5);
Tang Yuantianecfb4592015-09-07 16:23:16 +0800171 writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
Tang Yuantian386dc3b2017-01-20 14:59:34 +0800172 if (qpriv->is_dmacoherent)
173 writel(AHCI_PORT_AXICC_CFG,
174 reg_base + LS1021A_AXICC_ADDR);
Tang Yuantianecfb4592015-09-07 16:23:16 +0800175 break;
176
177 case AHCI_LS1043A:
Tang Yuantian01f29012017-01-20 14:59:35 +0800178 if (!qpriv->ecc_addr)
179 return -EINVAL;
180 writel(ECC_DIS_ARMV8_CH2, qpriv->ecc_addr);
Tang Yuantianef0cc7f2015-12-16 13:43:49 +0800181 writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
Tang Yuantianef0cc7f2015-12-16 13:43:49 +0800182 writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
Tang Yuantian386dc3b2017-01-20 14:59:34 +0800183 if (qpriv->is_dmacoherent)
184 writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC);
Tang Yuantianef0cc7f2015-12-16 13:43:49 +0800185 break;
186
Tang Yuantiand19f9aa2015-10-29 14:22:15 +0800187 case AHCI_LS2080A:
Tang Yuantianecfb4592015-09-07 16:23:16 +0800188 writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
Tang Yuantiane3a6dad2015-12-16 13:43:50 +0800189 writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
Tang Yuantian386dc3b2017-01-20 14:59:34 +0800190 if (qpriv->is_dmacoherent)
191 writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC);
Tang Yuantianecfb4592015-09-07 16:23:16 +0800192 break;
Tang Yuantian2facc6d2016-10-09 16:51:04 +0800193
194 case AHCI_LS1046A:
Tang Yuantian01f29012017-01-20 14:59:35 +0800195 if (!qpriv->ecc_addr)
196 return -EINVAL;
197 writel(ECC_DIS_ARMV8_CH2, qpriv->ecc_addr);
Tang Yuantian2facc6d2016-10-09 16:51:04 +0800198 writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
199 writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
Tang Yuantian386dc3b2017-01-20 14:59:34 +0800200 if (qpriv->is_dmacoherent)
201 writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC);
Tang Yuantian2facc6d2016-10-09 16:51:04 +0800202 break;
Tang Yuantiance8f4532017-01-20 14:59:36 +0800203
204 case AHCI_LS2088A:
205 writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1);
206 writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS);
207 if (qpriv->is_dmacoherent)
208 writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC);
209 break;
Tang Yuantianecfb4592015-09-07 16:23:16 +0800210 }
211
212 return 0;
213}
214
215static int ahci_qoriq_probe(struct platform_device *pdev)
216{
217 struct device_node *np = pdev->dev.of_node;
218 struct device *dev = &pdev->dev;
219 struct ahci_host_priv *hpriv;
220 struct ahci_qoriq_priv *qoriq_priv;
221 const struct of_device_id *of_id;
222 struct resource *res;
223 int rc;
224
225 hpriv = ahci_platform_get_resources(pdev);
226 if (IS_ERR(hpriv))
227 return PTR_ERR(hpriv);
228
229 of_id = of_match_node(ahci_qoriq_of_match, np);
230 if (!of_id)
231 return -ENODEV;
232
233 qoriq_priv = devm_kzalloc(dev, sizeof(*qoriq_priv), GFP_KERNEL);
234 if (!qoriq_priv)
235 return -ENOMEM;
236
237 qoriq_priv->type = (enum ahci_qoriq_type)of_id->data;
238
Tang Yuantian2facc6d2016-10-09 16:51:04 +0800239 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
240 "sata-ecc");
241 if (res) {
Tang Yuantianecfb4592015-09-07 16:23:16 +0800242 qoriq_priv->ecc_addr = devm_ioremap_resource(dev, res);
243 if (IS_ERR(qoriq_priv->ecc_addr))
244 return PTR_ERR(qoriq_priv->ecc_addr);
245 }
Tang Yuantian386dc3b2017-01-20 14:59:34 +0800246 qoriq_priv->is_dmacoherent = of_dma_is_coherent(np);
Tang Yuantianecfb4592015-09-07 16:23:16 +0800247
248 rc = ahci_platform_enable_resources(hpriv);
249 if (rc)
250 return rc;
251
252 hpriv->plat_data = qoriq_priv;
253 rc = ahci_qoriq_phy_init(hpriv);
254 if (rc)
255 goto disable_resources;
256
Tang Yuantianecfb4592015-09-07 16:23:16 +0800257 rc = ahci_platform_init_host(pdev, hpriv, &ahci_qoriq_port_info,
258 &ahci_qoriq_sht);
259 if (rc)
260 goto disable_resources;
261
262 return 0;
263
264disable_resources:
265 ahci_platform_disable_resources(hpriv);
266
267 return rc;
268}
269
270#ifdef CONFIG_PM_SLEEP
271static int ahci_qoriq_resume(struct device *dev)
272{
273 struct ata_host *host = dev_get_drvdata(dev);
274 struct ahci_host_priv *hpriv = host->private_data;
275 int rc;
276
277 rc = ahci_platform_enable_resources(hpriv);
278 if (rc)
279 return rc;
280
281 rc = ahci_qoriq_phy_init(hpriv);
282 if (rc)
283 goto disable_resources;
284
285 rc = ahci_platform_resume_host(dev);
286 if (rc)
287 goto disable_resources;
288
289 /* We resumed so update PM runtime state */
290 pm_runtime_disable(dev);
291 pm_runtime_set_active(dev);
292 pm_runtime_enable(dev);
293
294 return 0;
295
296disable_resources:
297 ahci_platform_disable_resources(hpriv);
298
299 return rc;
300}
301#endif
302
303static SIMPLE_DEV_PM_OPS(ahci_qoriq_pm_ops, ahci_platform_suspend,
304 ahci_qoriq_resume);
305
306static struct platform_driver ahci_qoriq_driver = {
307 .probe = ahci_qoriq_probe,
308 .remove = ata_platform_remove_one,
309 .driver = {
310 .name = DRV_NAME,
311 .of_match_table = ahci_qoriq_of_match,
312 .pm = &ahci_qoriq_pm_ops,
313 },
314};
315module_platform_driver(ahci_qoriq_driver);
316
317MODULE_DESCRIPTION("Freescale QorIQ AHCI SATA platform driver");
318MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>");
319MODULE_LICENSE("GPL");