blob: 84c7805c0247329b6e827270ae8c0ebb6b08fadc [file] [log] [blame]
Bartlomiej Zolnierkiewiczae8723f2014-03-25 19:51:40 +01001/*
2 * DaVinci DA850 AHCI SATA platform driver
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/pm.h>
13#include <linux/device.h>
14#include <linux/platform_device.h>
15#include <linux/libata.h>
16#include <linux/ahci_platform.h>
17#include "ahci.h"
18
Akinobu Mita018d5ef2015-01-29 08:30:29 +090019#define DRV_NAME "ahci_da850"
20
Bartlomiej Zolnierkiewiczae8723f2014-03-25 19:51:40 +010021/* SATA PHY Control Register offset from AHCI base */
22#define SATA_P0PHYCR_REG 0x178
23
24#define SATA_PHY_MPY(x) ((x) << 0)
25#define SATA_PHY_LOS(x) ((x) << 6)
26#define SATA_PHY_RXCDR(x) ((x) << 10)
27#define SATA_PHY_RXEQ(x) ((x) << 13)
28#define SATA_PHY_TXSWING(x) ((x) << 19)
29#define SATA_PHY_ENPLL(x) ((x) << 31)
30
31/*
32 * The multiplier needed for 1.5GHz PLL output.
33 *
34 * NOTE: This is currently hardcoded to be suitable for 100MHz crystal
35 * frequency (which is used by DA850 EVM board) and may need to be changed
36 * if you would like to use this driver on some other board.
37 */
38#define DA850_SATA_CLK_MULTIPLIER 7
39
40static void da850_sata_init(struct device *dev, void __iomem *pwrdn_reg,
41 void __iomem *ahci_base)
42{
43 unsigned int val;
44
45 /* Enable SATA clock receiver */
46 val = readl(pwrdn_reg);
47 val &= ~BIT(0);
48 writel(val, pwrdn_reg);
49
50 val = SATA_PHY_MPY(DA850_SATA_CLK_MULTIPLIER + 1) | SATA_PHY_LOS(1) |
51 SATA_PHY_RXCDR(4) | SATA_PHY_RXEQ(1) | SATA_PHY_TXSWING(3) |
52 SATA_PHY_ENPLL(1);
53
54 writel(val, ahci_base + SATA_P0PHYCR_REG);
55}
56
Bartosz Golaszewskif4d435f2017-01-30 11:02:05 +010057static int ahci_da850_softreset(struct ata_link *link,
58 unsigned int *class, unsigned long deadline)
59{
60 int pmp, ret;
61
62 pmp = sata_srst_pmp(link);
63
64 /*
65 * There's an issue with the SATA controller on da850 SoCs: if we
66 * enable Port Multiplier support, but the drive is connected directly
67 * to the board, it can't be detected. As a workaround: if PMP is
68 * enabled, we first call ahci_do_softreset() and pass it the result of
69 * sata_srst_pmp(). If this call fails, we retry with pmp = 0.
70 */
71 ret = ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
72 if (pmp && ret == -EBUSY)
73 return ahci_do_softreset(link, class, 0,
74 deadline, ahci_check_ready);
75
76 return ret;
77}
78
79static struct ata_port_operations ahci_da850_port_ops = {
80 .inherits = &ahci_platform_ops,
81 .softreset = ahci_da850_softreset,
82 /*
83 * No need to override .pmp_softreset - it's only used for actual
84 * PMP-enabled ports.
85 */
86};
87
Bartlomiej Zolnierkiewiczae8723f2014-03-25 19:51:40 +010088static const struct ata_port_info ahci_da850_port_info = {
89 .flags = AHCI_FLAG_COMMON,
90 .pio_mask = ATA_PIO4,
91 .udma_mask = ATA_UDMA6,
Bartosz Golaszewskif4d435f2017-01-30 11:02:05 +010092 .port_ops = &ahci_da850_port_ops,
Bartlomiej Zolnierkiewiczae8723f2014-03-25 19:51:40 +010093};
94
Akinobu Mita018d5ef2015-01-29 08:30:29 +090095static struct scsi_host_template ahci_platform_sht = {
96 AHCI_SHT(DRV_NAME),
97};
98
Bartlomiej Zolnierkiewiczae8723f2014-03-25 19:51:40 +010099static int ahci_da850_probe(struct platform_device *pdev)
100{
101 struct device *dev = &pdev->dev;
102 struct ahci_host_priv *hpriv;
103 struct resource *res;
104 void __iomem *pwrdn_reg;
Bartosz Golaszewski82dbe1a2017-01-30 11:02:01 +0100105 struct clk *clk;
Bartlomiej Zolnierkiewiczae8723f2014-03-25 19:51:40 +0100106 int rc;
107
108 hpriv = ahci_platform_get_resources(pdev);
109 if (IS_ERR(hpriv))
110 return PTR_ERR(hpriv);
111
Bartosz Golaszewski82dbe1a2017-01-30 11:02:01 +0100112 /*
113 * Internally ahci_platform_get_resources() calls clk_get(dev, NULL)
114 * when trying to obtain the functional clock. This SATA controller
115 * uses two clocks for which we specify two connection ids. If we don't
116 * have the functional clock at this point - call clk_get() again with
117 * con_id = "fck".
118 */
119 if (!hpriv->clks[0]) {
120 clk = clk_get(dev, "fck");
121 if (IS_ERR(clk))
122 return PTR_ERR(clk);
123
124 hpriv->clks[0] = clk;
125 }
126
Bartlomiej Zolnierkiewiczae8723f2014-03-25 19:51:40 +0100127 rc = ahci_platform_enable_resources(hpriv);
128 if (rc)
129 return rc;
130
131 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
132 if (!res)
133 goto disable_resources;
134
135 pwrdn_reg = devm_ioremap(dev, res->start, resource_size(res));
136 if (!pwrdn_reg)
137 goto disable_resources;
138
139 da850_sata_init(dev, pwrdn_reg, hpriv->mmio);
140
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900141 rc = ahci_platform_init_host(pdev, hpriv, &ahci_da850_port_info,
142 &ahci_platform_sht);
Bartlomiej Zolnierkiewiczae8723f2014-03-25 19:51:40 +0100143 if (rc)
144 goto disable_resources;
145
146 return 0;
147disable_resources:
148 ahci_platform_disable_resources(hpriv);
149 return rc;
150}
151
152static SIMPLE_DEV_PM_OPS(ahci_da850_pm_ops, ahci_platform_suspend,
153 ahci_platform_resume);
154
Bartosz Golaszewskibf4ae3f2017-01-30 11:02:04 +0100155static const struct of_device_id ahci_da850_of_match[] = {
156 { .compatible = "ti,da850-ahci", },
157 { },
158};
159MODULE_DEVICE_TABLE(of, ahci_da850_of_match);
160
Bartlomiej Zolnierkiewiczae8723f2014-03-25 19:51:40 +0100161static struct platform_driver ahci_da850_driver = {
162 .probe = ahci_da850_probe,
163 .remove = ata_platform_remove_one,
164 .driver = {
Akinobu Mita018d5ef2015-01-29 08:30:29 +0900165 .name = DRV_NAME,
Bartosz Golaszewskibf4ae3f2017-01-30 11:02:04 +0100166 .of_match_table = ahci_da850_of_match,
Bartlomiej Zolnierkiewiczae8723f2014-03-25 19:51:40 +0100167 .pm = &ahci_da850_pm_ops,
168 },
169};
170module_platform_driver(ahci_da850_driver);
171
172MODULE_DESCRIPTION("DaVinci DA850 AHCI SATA platform driver");
173MODULE_AUTHOR("Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>");
174MODULE_LICENSE("GPL");