blob: e9c7c07fd84c8f063d317aadd54e321afac327d3 [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Suneel Garapatia73ed352015-06-09 14:23:50 +05302/*
3 * Copyright (C) 2015 Xilinx, Inc.
4 * CEVA AHCI SATA platform driver
5 *
6 * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov
Suneel Garapatia73ed352015-06-09 14:23:50 +05307 */
8
9#include <linux/ahci_platform.h>
10#include <linux/kernel.h>
11#include <linux/libata.h>
12#include <linux/module.h>
13#include <linux/of_device.h>
14#include <linux/platform_device.h>
Piyush Mehta9a9d3ab2021-02-08 23:33:56 +053015#include <linux/reset.h>
Suneel Garapatia73ed352015-06-09 14:23:50 +053016#include "ahci.h"
17
18/* Vendor Specific Register Offsets */
19#define AHCI_VEND_PCFG 0xA4
20#define AHCI_VEND_PPCFG 0xA8
21#define AHCI_VEND_PP2C 0xAC
22#define AHCI_VEND_PP3C 0xB0
23#define AHCI_VEND_PP4C 0xB4
24#define AHCI_VEND_PP5C 0xB8
Anurag Kumar Vulisha3bc867d2017-08-21 13:17:21 +020025#define AHCI_VEND_AXICC 0xBC
Suneel Garapatia73ed352015-06-09 14:23:50 +053026#define AHCI_VEND_PAXIC 0xC0
27#define AHCI_VEND_PTC 0xC8
28
29/* Vendor Specific Register bit definitions */
30#define PAXIC_ADBW_BW64 0x1
Anurag Kumar Vulishaf0a559a2017-08-21 13:17:24 +020031#define PAXIC_MAWID(i) (((i) * 2) << 4)
32#define PAXIC_MARID(i) (((i) * 2) << 12)
33#define PAXIC_MARIDD(i) ((((i) * 2) + 1) << 16)
34#define PAXIC_MAWIDD(i) ((((i) * 2) + 1) << 8)
Suneel Garapatia73ed352015-06-09 14:23:50 +053035#define PAXIC_OTL (0x4 << 20)
36
Anurag Kumar Vulisha3bc867d2017-08-21 13:17:21 +020037/* Register bit definitions for cache control */
38#define AXICC_ARCA_VAL (0xF << 0)
39#define AXICC_ARCF_VAL (0xF << 4)
40#define AXICC_ARCH_VAL (0xF << 8)
41#define AXICC_ARCP_VAL (0xF << 12)
42#define AXICC_AWCFD_VAL (0xF << 16)
43#define AXICC_AWCD_VAL (0xF << 20)
44#define AXICC_AWCF_VAL (0xF << 24)
45
Suneel Garapatia73ed352015-06-09 14:23:50 +053046#define PCFG_TPSS_VAL (0x32 << 16)
47#define PCFG_TPRS_VAL (0x2 << 12)
48#define PCFG_PAD_VAL 0x2
49
50#define PPCFG_TTA 0x1FFFE
51#define PPCFG_PSSO_EN (1 << 28)
52#define PPCFG_PSS_EN (1 << 29)
53#define PPCFG_ESDF_EN (1 << 31)
54
Suneel Garapatia73ed352015-06-09 14:23:50 +053055#define PP5C_RIT 0x60216
56#define PP5C_RCT (0x7f0 << 20)
57
58#define PTC_RX_WM_VAL 0x40
59#define PTC_RSVD (1 << 27)
60
61#define PORT0_BASE 0x100
62#define PORT1_BASE 0x180
63
64/* Port Control Register Bit Definitions */
Anurag Kumar Vulishae8fc8b82017-08-21 13:17:18 +020065#define PORT_SCTL_SPD_GEN3 (0x3 << 4)
Suneel Garapatia73ed352015-06-09 14:23:50 +053066#define PORT_SCTL_SPD_GEN2 (0x2 << 4)
67#define PORT_SCTL_SPD_GEN1 (0x1 << 4)
68#define PORT_SCTL_IPM (0x3 << 8)
69
70#define PORT_BASE 0x100
71#define PORT_OFFSET 0x80
72#define NR_PORTS 2
73#define DRV_NAME "ahci-ceva"
74#define CEVA_FLAG_BROKEN_GEN2 1
75
Anurag Kumar Vulisha05e890d2017-08-21 13:17:20 +020076static unsigned int rx_watermark = PTC_RX_WM_VAL;
77module_param(rx_watermark, uint, 0644);
78MODULE_PARM_DESC(rx_watermark, "RxWaterMark value (0 - 0x80)");
79
Suneel Garapatia73ed352015-06-09 14:23:50 +053080struct ceva_ahci_priv {
81 struct platform_device *ahci_pdev;
Anurag Kumar Vulishafe8365b2017-08-21 13:17:17 +020082 /* Port Phy2Cfg Register */
83 u32 pp2c[NR_PORTS];
84 u32 pp3c[NR_PORTS];
85 u32 pp4c[NR_PORTS];
86 u32 pp5c[NR_PORTS];
Anurag Kumar Vulisha3bc867d2017-08-21 13:17:21 +020087 /* Axi Cache Control Register */
88 u32 axicc;
89 bool is_cci_enabled;
Suneel Garapatia73ed352015-06-09 14:23:50 +053090 int flags;
Piyush Mehta9a9d3ab2021-02-08 23:33:56 +053091 struct reset_control *rst;
Suneel Garapatia73ed352015-06-09 14:23:50 +053092};
93
Anurag Kumar Vulishaff0d6372017-08-21 13:17:19 +020094static unsigned int ceva_ahci_read_id(struct ata_device *dev,
95 struct ata_taskfile *tf, u16 *id)
96{
Damien Le Moal815b6cb2021-12-02 15:27:08 +090097 __le16 *__id = (__le16 *)id;
Anurag Kumar Vulishaff0d6372017-08-21 13:17:19 +020098 u32 err_mask;
99
100 err_mask = ata_do_dev_read_id(dev, tf, id);
101 if (err_mask)
102 return err_mask;
103 /*
104 * Since CEVA controller does not support device sleep feature, we
105 * need to clear DEVSLP (bit 8) in word78 of the IDENTIFY DEVICE data.
106 */
Damien Le Moal815b6cb2021-12-02 15:27:08 +0900107 __id[ATA_ID_FEATURE_SUPP] &= cpu_to_le16(~(1 << 8));
Anurag Kumar Vulishaff0d6372017-08-21 13:17:19 +0200108
109 return 0;
110}
111
Suneel Garapatia73ed352015-06-09 14:23:50 +0530112static struct ata_port_operations ahci_ceva_ops = {
113 .inherits = &ahci_platform_ops,
Anurag Kumar Vulishaff0d6372017-08-21 13:17:19 +0200114 .read_id = ceva_ahci_read_id,
Suneel Garapatia73ed352015-06-09 14:23:50 +0530115};
116
117static const struct ata_port_info ahci_ceva_port_info = {
118 .flags = AHCI_FLAG_COMMON,
119 .pio_mask = ATA_PIO4,
120 .udma_mask = ATA_UDMA6,
121 .port_ops = &ahci_ceva_ops,
122};
123
124static void ahci_ceva_setup(struct ahci_host_priv *hpriv)
125{
126 void __iomem *mmio = hpriv->mmio;
127 struct ceva_ahci_priv *cevapriv = hpriv->plat_data;
128 u32 tmp;
129 int i;
130
Suneel Garapatia73ed352015-06-09 14:23:50 +0530131 /* Set AHCI Enable */
132 tmp = readl(mmio + HOST_CTL);
133 tmp |= HOST_AHCI_EN;
134 writel(tmp, mmio + HOST_CTL);
135
136 for (i = 0; i < NR_PORTS; i++) {
137 /* TPSS TPRS scalars, CISE and Port Addr */
138 tmp = PCFG_TPSS_VAL | PCFG_TPRS_VAL | (PCFG_PAD_VAL + i);
139 writel(tmp, mmio + AHCI_VEND_PCFG);
140
Anurag Kumar Vulisha6e037fb2017-08-21 13:17:22 +0200141 /*
142 * AXI Data bus width to 64
143 * Set Mem Addr Read, Write ID for data transfers
Anurag Kumar Vulishaf0a559a2017-08-21 13:17:24 +0200144 * Set Mem Addr Read ID, Write ID for non-data transfers
Anurag Kumar Vulisha6e037fb2017-08-21 13:17:22 +0200145 * Transfer limit to 72 DWord
146 */
Anurag Kumar Vulishaf0a559a2017-08-21 13:17:24 +0200147 tmp = PAXIC_ADBW_BW64 | PAXIC_MAWIDD(i) | PAXIC_MARIDD(i) |
148 PAXIC_MAWID(i) | PAXIC_MARID(i) | PAXIC_OTL;
Anurag Kumar Vulisha6e037fb2017-08-21 13:17:22 +0200149 writel(tmp, mmio + AHCI_VEND_PAXIC);
150
Anurag Kumar Vulisha3bc867d2017-08-21 13:17:21 +0200151 /* Set AXI cache control register if CCi is enabled */
152 if (cevapriv->is_cci_enabled) {
153 tmp = readl(mmio + AHCI_VEND_AXICC);
154 tmp |= AXICC_ARCA_VAL | AXICC_ARCF_VAL |
155 AXICC_ARCH_VAL | AXICC_ARCP_VAL |
156 AXICC_AWCFD_VAL | AXICC_AWCD_VAL |
157 AXICC_AWCF_VAL;
158 writel(tmp, mmio + AHCI_VEND_AXICC);
159 }
160
Suneel Garapatia73ed352015-06-09 14:23:50 +0530161 /* Port Phy Cfg register enables */
162 tmp = PPCFG_TTA | PPCFG_PSS_EN | PPCFG_ESDF_EN;
163 writel(tmp, mmio + AHCI_VEND_PPCFG);
164
165 /* Phy Control OOB timing parameters COMINIT */
Anurag Kumar Vulishafe8365b2017-08-21 13:17:17 +0200166 writel(cevapriv->pp2c[i], mmio + AHCI_VEND_PP2C);
Suneel Garapatia73ed352015-06-09 14:23:50 +0530167
168 /* Phy Control OOB timing parameters COMWAKE */
Anurag Kumar Vulishafe8365b2017-08-21 13:17:17 +0200169 writel(cevapriv->pp3c[i], mmio + AHCI_VEND_PP3C);
Suneel Garapatia73ed352015-06-09 14:23:50 +0530170
171 /* Phy Control Burst timing setting */
Anurag Kumar Vulishafe8365b2017-08-21 13:17:17 +0200172 writel(cevapriv->pp4c[i], mmio + AHCI_VEND_PP4C);
Suneel Garapatia73ed352015-06-09 14:23:50 +0530173
174 /* Rate Change Timer and Retry Interval Timer setting */
Anurag Kumar Vulishafe8365b2017-08-21 13:17:17 +0200175 writel(cevapriv->pp5c[i], mmio + AHCI_VEND_PP5C);
Suneel Garapatia73ed352015-06-09 14:23:50 +0530176
177 /* Rx Watermark setting */
Anurag Kumar Vulisha05e890d2017-08-21 13:17:20 +0200178 tmp = rx_watermark | PTC_RSVD;
Suneel Garapatia73ed352015-06-09 14:23:50 +0530179 writel(tmp, mmio + AHCI_VEND_PTC);
180
Anurag Kumar Vulishae8fc8b82017-08-21 13:17:18 +0200181 /* Default to Gen 3 Speed and Gen 1 if Gen2 is broken */
182 tmp = PORT_SCTL_SPD_GEN3 | PORT_SCTL_IPM;
Suneel Garapatia73ed352015-06-09 14:23:50 +0530183 if (cevapriv->flags & CEVA_FLAG_BROKEN_GEN2)
184 tmp = PORT_SCTL_SPD_GEN1 | PORT_SCTL_IPM;
185 writel(tmp, mmio + PORT_SCR_CTL + PORT_BASE + PORT_OFFSET * i);
186 }
187}
188
189static struct scsi_host_template ahci_platform_sht = {
190 AHCI_SHT(DRV_NAME),
191};
192
193static int ceva_ahci_probe(struct platform_device *pdev)
194{
195 struct device_node *np = pdev->dev.of_node;
196 struct device *dev = &pdev->dev;
197 struct ahci_host_priv *hpriv;
198 struct ceva_ahci_priv *cevapriv;
Anurag Kumar Vulisha3bc867d2017-08-21 13:17:21 +0200199 enum dev_dma_attr attr;
Suneel Garapatia73ed352015-06-09 14:23:50 +0530200 int rc;
201
202 cevapriv = devm_kzalloc(dev, sizeof(*cevapriv), GFP_KERNEL);
203 if (!cevapriv)
204 return -ENOMEM;
205
206 cevapriv->ahci_pdev = pdev;
207
Piyush Mehta9a9d3ab2021-02-08 23:33:56 +0530208 cevapriv->rst = devm_reset_control_get_optional_exclusive(&pdev->dev,
209 NULL);
Piyush Mehtafa4b42b2021-03-05 14:40:29 +0530210 if (IS_ERR(cevapriv->rst))
211 dev_err_probe(&pdev->dev, PTR_ERR(cevapriv->rst),
212 "failed to get reset\n");
Piyush Mehta9a9d3ab2021-02-08 23:33:56 +0530213
Kunihiko Hayashi16af2d62018-08-22 21:13:01 +0900214 hpriv = ahci_platform_get_resources(pdev, 0);
Suneel Garapatia73ed352015-06-09 14:23:50 +0530215 if (IS_ERR(hpriv))
216 return PTR_ERR(hpriv);
217
Piyush Mehta9a9d3ab2021-02-08 23:33:56 +0530218 if (!cevapriv->rst) {
219 rc = ahci_platform_enable_resources(hpriv);
220 if (rc)
221 return rc;
222 } else {
223 int i;
224
225 rc = ahci_platform_enable_clks(hpriv);
226 if (rc)
227 return rc;
228 /* Assert the controller reset */
229 reset_control_assert(cevapriv->rst);
230
231 for (i = 0; i < hpriv->nports; i++) {
232 rc = phy_init(hpriv->phys[i]);
233 if (rc)
234 return rc;
235 }
236
237 /* De-assert the controller reset */
238 reset_control_deassert(cevapriv->rst);
239
240 for (i = 0; i < hpriv->nports; i++) {
241 rc = phy_power_on(hpriv->phys[i]);
242 if (rc) {
243 phy_exit(hpriv->phys[i]);
244 return rc;
245 }
246 }
247 }
Suneel Garapatia73ed352015-06-09 14:23:50 +0530248
249 if (of_property_read_bool(np, "ceva,broken-gen2"))
250 cevapriv->flags = CEVA_FLAG_BROKEN_GEN2;
251
Anurag Kumar Vulishafe8365b2017-08-21 13:17:17 +0200252 /* Read OOB timing value for COMINIT from device-tree */
253 if (of_property_read_u8_array(np, "ceva,p0-cominit-params",
254 (u8 *)&cevapriv->pp2c[0], 4) < 0) {
255 dev_warn(dev, "ceva,p0-cominit-params property not defined\n");
256 return -EINVAL;
257 }
258
259 if (of_property_read_u8_array(np, "ceva,p1-cominit-params",
260 (u8 *)&cevapriv->pp2c[1], 4) < 0) {
261 dev_warn(dev, "ceva,p1-cominit-params property not defined\n");
262 return -EINVAL;
263 }
264
265 /* Read OOB timing value for COMWAKE from device-tree*/
266 if (of_property_read_u8_array(np, "ceva,p0-comwake-params",
267 (u8 *)&cevapriv->pp3c[0], 4) < 0) {
268 dev_warn(dev, "ceva,p0-comwake-params property not defined\n");
269 return -EINVAL;
270 }
271
272 if (of_property_read_u8_array(np, "ceva,p1-comwake-params",
273 (u8 *)&cevapriv->pp3c[1], 4) < 0) {
274 dev_warn(dev, "ceva,p1-comwake-params property not defined\n");
275 return -EINVAL;
276 }
277
278 /* Read phy BURST timing value from device-tree */
279 if (of_property_read_u8_array(np, "ceva,p0-burst-params",
280 (u8 *)&cevapriv->pp4c[0], 4) < 0) {
281 dev_warn(dev, "ceva,p0-burst-params property not defined\n");
282 return -EINVAL;
283 }
284
285 if (of_property_read_u8_array(np, "ceva,p1-burst-params",
286 (u8 *)&cevapriv->pp4c[1], 4) < 0) {
287 dev_warn(dev, "ceva,p1-burst-params property not defined\n");
288 return -EINVAL;
289 }
290
291 /* Read phy RETRY interval timing value from device-tree */
292 if (of_property_read_u16_array(np, "ceva,p0-retry-params",
293 (u16 *)&cevapriv->pp5c[0], 2) < 0) {
294 dev_warn(dev, "ceva,p0-retry-params property not defined\n");
295 return -EINVAL;
296 }
297
298 if (of_property_read_u16_array(np, "ceva,p1-retry-params",
299 (u16 *)&cevapriv->pp5c[1], 2) < 0) {
300 dev_warn(dev, "ceva,p1-retry-params property not defined\n");
301 return -EINVAL;
302 }
303
Anurag Kumar Vulisha3bc867d2017-08-21 13:17:21 +0200304 /*
305 * Check if CCI is enabled for SATA. The DEV_DMA_COHERENT is returned
306 * if CCI is enabled, so check for DEV_DMA_COHERENT.
307 */
308 attr = device_get_dma_attr(dev);
309 cevapriv->is_cci_enabled = (attr == DEV_DMA_COHERENT);
310
Suneel Garapatia73ed352015-06-09 14:23:50 +0530311 hpriv->plat_data = cevapriv;
312
313 /* CEVA specific initialization */
314 ahci_ceva_setup(hpriv);
315
316 rc = ahci_platform_init_host(pdev, hpriv, &ahci_ceva_port_info,
317 &ahci_platform_sht);
318 if (rc)
319 goto disable_resources;
320
321 return 0;
322
323disable_resources:
324 ahci_platform_disable_resources(hpriv);
325 return rc;
326}
327
328static int __maybe_unused ceva_ahci_suspend(struct device *dev)
329{
Anurag Kumar Vulisha26bf3b62017-08-21 13:17:23 +0200330 return ahci_platform_suspend(dev);
Suneel Garapatia73ed352015-06-09 14:23:50 +0530331}
332
333static int __maybe_unused ceva_ahci_resume(struct device *dev)
334{
Anurag Kumar Vulisha26bf3b62017-08-21 13:17:23 +0200335 struct ata_host *host = dev_get_drvdata(dev);
336 struct ahci_host_priv *hpriv = host->private_data;
337 int rc;
338
339 rc = ahci_platform_enable_resources(hpriv);
340 if (rc)
341 return rc;
342
343 /* Configure CEVA specific config before resuming HBA */
344 ahci_ceva_setup(hpriv);
345
346 rc = ahci_platform_resume_host(dev);
347 if (rc)
348 goto disable_resources;
349
350 /* We resumed so update PM runtime state */
351 pm_runtime_disable(dev);
352 pm_runtime_set_active(dev);
353 pm_runtime_enable(dev);
354
355 return 0;
356
357disable_resources:
358 ahci_platform_disable_resources(hpriv);
359
360 return rc;
Suneel Garapatia73ed352015-06-09 14:23:50 +0530361}
362
363static SIMPLE_DEV_PM_OPS(ahci_ceva_pm_ops, ceva_ahci_suspend, ceva_ahci_resume);
364
365static const struct of_device_id ceva_ahci_of_match[] = {
366 { .compatible = "ceva,ahci-1v84" },
367 {},
368};
369MODULE_DEVICE_TABLE(of, ceva_ahci_of_match);
370
371static struct platform_driver ceva_ahci_driver = {
372 .probe = ceva_ahci_probe,
373 .remove = ata_platform_remove_one,
374 .driver = {
375 .name = DRV_NAME,
376 .of_match_table = ceva_ahci_of_match,
377 .pm = &ahci_ceva_pm_ops,
378 },
379};
380module_platform_driver(ceva_ahci_driver);
381
382MODULE_DESCRIPTION("CEVA AHCI SATA platform driver");
383MODULE_AUTHOR("Xilinx Inc.");
384MODULE_LICENSE("GPL v2");