1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * DaVinci DA850 AHCI SATA platform driver
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/kernel.h>
7*4882a593Smuzhiyun #include <linux/module.h>
8*4882a593Smuzhiyun #include <linux/pm.h>
9*4882a593Smuzhiyun #include <linux/device.h>
10*4882a593Smuzhiyun #include <linux/platform_device.h>
11*4882a593Smuzhiyun #include <linux/libata.h>
12*4882a593Smuzhiyun #include <linux/ahci_platform.h>
13*4882a593Smuzhiyun #include "ahci.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #define DRV_NAME "ahci_da850"
16*4882a593Smuzhiyun #define HARDRESET_RETRIES 5
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /* SATA PHY Control Register offset from AHCI base */
19*4882a593Smuzhiyun #define SATA_P0PHYCR_REG 0x178
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define SATA_PHY_MPY(x) ((x) << 0)
22*4882a593Smuzhiyun #define SATA_PHY_LOS(x) ((x) << 6)
23*4882a593Smuzhiyun #define SATA_PHY_RXCDR(x) ((x) << 10)
24*4882a593Smuzhiyun #define SATA_PHY_RXEQ(x) ((x) << 13)
25*4882a593Smuzhiyun #define SATA_PHY_TXSWING(x) ((x) << 19)
26*4882a593Smuzhiyun #define SATA_PHY_ENPLL(x) ((x) << 31)
27*4882a593Smuzhiyun
da850_sata_init(struct device * dev,void __iomem * pwrdn_reg,void __iomem * ahci_base,u32 mpy)28*4882a593Smuzhiyun static void da850_sata_init(struct device *dev, void __iomem *pwrdn_reg,
29*4882a593Smuzhiyun void __iomem *ahci_base, u32 mpy)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun unsigned int val;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /* Enable SATA clock receiver */
34*4882a593Smuzhiyun val = readl(pwrdn_reg);
35*4882a593Smuzhiyun val &= ~BIT(0);
36*4882a593Smuzhiyun writel(val, pwrdn_reg);
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun val = SATA_PHY_MPY(mpy) | SATA_PHY_LOS(1) | SATA_PHY_RXCDR(4) |
39*4882a593Smuzhiyun SATA_PHY_RXEQ(1) | SATA_PHY_TXSWING(3) | SATA_PHY_ENPLL(1);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun writel(val, ahci_base + SATA_P0PHYCR_REG);
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
ahci_da850_calculate_mpy(unsigned long refclk_rate)44*4882a593Smuzhiyun static u32 ahci_da850_calculate_mpy(unsigned long refclk_rate)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun u32 pll_output = 1500000000, needed;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun * We need to determine the value of the multiplier (MPY) bits.
50*4882a593Smuzhiyun * In order to include the 12.5 multiplier we need to first divide
51*4882a593Smuzhiyun * the refclk rate by ten.
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * __div64_32() turned out to be unreliable, sometimes returning
54*4882a593Smuzhiyun * false results.
55*4882a593Smuzhiyun */
56*4882a593Smuzhiyun WARN((refclk_rate % 10) != 0, "refclk must be divisible by 10");
57*4882a593Smuzhiyun needed = pll_output / (refclk_rate / 10);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun * What we have now is (multiplier * 10).
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * Let's determine the actual register value we need to write.
63*4882a593Smuzhiyun */
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun switch (needed) {
66*4882a593Smuzhiyun case 50:
67*4882a593Smuzhiyun return 0x1;
68*4882a593Smuzhiyun case 60:
69*4882a593Smuzhiyun return 0x2;
70*4882a593Smuzhiyun case 80:
71*4882a593Smuzhiyun return 0x4;
72*4882a593Smuzhiyun case 100:
73*4882a593Smuzhiyun return 0x5;
74*4882a593Smuzhiyun case 120:
75*4882a593Smuzhiyun return 0x6;
76*4882a593Smuzhiyun case 125:
77*4882a593Smuzhiyun return 0x7;
78*4882a593Smuzhiyun case 150:
79*4882a593Smuzhiyun return 0x8;
80*4882a593Smuzhiyun case 200:
81*4882a593Smuzhiyun return 0x9;
82*4882a593Smuzhiyun case 250:
83*4882a593Smuzhiyun return 0xa;
84*4882a593Smuzhiyun default:
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun * We should have divided evenly - if not, return an invalid
87*4882a593Smuzhiyun * value.
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
ahci_da850_softreset(struct ata_link * link,unsigned int * class,unsigned long deadline)93*4882a593Smuzhiyun static int ahci_da850_softreset(struct ata_link *link,
94*4882a593Smuzhiyun unsigned int *class, unsigned long deadline)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun int pmp, ret;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun pmp = sata_srst_pmp(link);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun * There's an issue with the SATA controller on da850 SoCs: if we
102*4882a593Smuzhiyun * enable Port Multiplier support, but the drive is connected directly
103*4882a593Smuzhiyun * to the board, it can't be detected. As a workaround: if PMP is
104*4882a593Smuzhiyun * enabled, we first call ahci_do_softreset() and pass it the result of
105*4882a593Smuzhiyun * sata_srst_pmp(). If this call fails, we retry with pmp = 0.
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun ret = ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
108*4882a593Smuzhiyun if (pmp && ret == -EBUSY)
109*4882a593Smuzhiyun return ahci_do_softreset(link, class, 0,
110*4882a593Smuzhiyun deadline, ahci_check_ready);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun return ret;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
ahci_da850_hardreset(struct ata_link * link,unsigned int * class,unsigned long deadline)115*4882a593Smuzhiyun static int ahci_da850_hardreset(struct ata_link *link,
116*4882a593Smuzhiyun unsigned int *class, unsigned long deadline)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun int ret, retry = HARDRESET_RETRIES;
119*4882a593Smuzhiyun bool online;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /*
122*4882a593Smuzhiyun * In order to correctly service the LCD controller of the da850 SoC,
123*4882a593Smuzhiyun * we increased the PLL0 frequency to 456MHz from the default 300MHz.
124*4882a593Smuzhiyun *
125*4882a593Smuzhiyun * This made the SATA controller unstable and the hardreset operation
126*4882a593Smuzhiyun * does not always succeed the first time. Before really giving up to
127*4882a593Smuzhiyun * bring up the link, retry the reset a couple times.
128*4882a593Smuzhiyun */
129*4882a593Smuzhiyun do {
130*4882a593Smuzhiyun ret = ahci_do_hardreset(link, class, deadline, &online);
131*4882a593Smuzhiyun if (online)
132*4882a593Smuzhiyun return ret;
133*4882a593Smuzhiyun } while (retry--);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun return ret;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun static struct ata_port_operations ahci_da850_port_ops = {
139*4882a593Smuzhiyun .inherits = &ahci_platform_ops,
140*4882a593Smuzhiyun .softreset = ahci_da850_softreset,
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun * No need to override .pmp_softreset - it's only used for actual
143*4882a593Smuzhiyun * PMP-enabled ports.
144*4882a593Smuzhiyun */
145*4882a593Smuzhiyun .hardreset = ahci_da850_hardreset,
146*4882a593Smuzhiyun .pmp_hardreset = ahci_da850_hardreset,
147*4882a593Smuzhiyun };
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun static const struct ata_port_info ahci_da850_port_info = {
150*4882a593Smuzhiyun .flags = AHCI_FLAG_COMMON,
151*4882a593Smuzhiyun .pio_mask = ATA_PIO4,
152*4882a593Smuzhiyun .udma_mask = ATA_UDMA6,
153*4882a593Smuzhiyun .port_ops = &ahci_da850_port_ops,
154*4882a593Smuzhiyun };
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun static struct scsi_host_template ahci_platform_sht = {
157*4882a593Smuzhiyun AHCI_SHT(DRV_NAME),
158*4882a593Smuzhiyun };
159*4882a593Smuzhiyun
ahci_da850_probe(struct platform_device * pdev)160*4882a593Smuzhiyun static int ahci_da850_probe(struct platform_device *pdev)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun struct device *dev = &pdev->dev;
163*4882a593Smuzhiyun struct ahci_host_priv *hpriv;
164*4882a593Smuzhiyun void __iomem *pwrdn_reg;
165*4882a593Smuzhiyun struct resource *res;
166*4882a593Smuzhiyun struct clk *clk;
167*4882a593Smuzhiyun u32 mpy;
168*4882a593Smuzhiyun int rc;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun hpriv = ahci_platform_get_resources(pdev, 0);
171*4882a593Smuzhiyun if (IS_ERR(hpriv))
172*4882a593Smuzhiyun return PTR_ERR(hpriv);
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /*
175*4882a593Smuzhiyun * Internally ahci_platform_get_resources() calls clk_get(dev, NULL)
176*4882a593Smuzhiyun * when trying to obtain the functional clock. This SATA controller
177*4882a593Smuzhiyun * uses two clocks for which we specify two connection ids. If we don't
178*4882a593Smuzhiyun * have the functional clock at this point - call clk_get() again with
179*4882a593Smuzhiyun * con_id = "fck".
180*4882a593Smuzhiyun */
181*4882a593Smuzhiyun if (!hpriv->clks[0]) {
182*4882a593Smuzhiyun clk = clk_get(dev, "fck");
183*4882a593Smuzhiyun if (IS_ERR(clk))
184*4882a593Smuzhiyun return PTR_ERR(clk);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun hpriv->clks[0] = clk;
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun /*
190*4882a593Smuzhiyun * The second clock used by ahci-da850 is the external REFCLK. If we
191*4882a593Smuzhiyun * didn't get it from ahci_platform_get_resources(), let's try to
192*4882a593Smuzhiyun * specify the con_id in clk_get().
193*4882a593Smuzhiyun */
194*4882a593Smuzhiyun if (!hpriv->clks[1]) {
195*4882a593Smuzhiyun clk = clk_get(dev, "refclk");
196*4882a593Smuzhiyun if (IS_ERR(clk)) {
197*4882a593Smuzhiyun dev_err(dev, "unable to obtain the reference clock");
198*4882a593Smuzhiyun return -ENODEV;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun hpriv->clks[1] = clk;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun mpy = ahci_da850_calculate_mpy(clk_get_rate(hpriv->clks[1]));
205*4882a593Smuzhiyun if (mpy == 0) {
206*4882a593Smuzhiyun dev_err(dev, "invalid REFCLK multiplier value: 0x%x", mpy);
207*4882a593Smuzhiyun return -EINVAL;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun rc = ahci_platform_enable_resources(hpriv);
211*4882a593Smuzhiyun if (rc)
212*4882a593Smuzhiyun return rc;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
215*4882a593Smuzhiyun if (!res) {
216*4882a593Smuzhiyun rc = -ENODEV;
217*4882a593Smuzhiyun goto disable_resources;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun pwrdn_reg = devm_ioremap(dev, res->start, resource_size(res));
221*4882a593Smuzhiyun if (!pwrdn_reg) {
222*4882a593Smuzhiyun rc = -ENOMEM;
223*4882a593Smuzhiyun goto disable_resources;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun da850_sata_init(dev, pwrdn_reg, hpriv->mmio, mpy);
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun rc = ahci_platform_init_host(pdev, hpriv, &ahci_da850_port_info,
229*4882a593Smuzhiyun &ahci_platform_sht);
230*4882a593Smuzhiyun if (rc)
231*4882a593Smuzhiyun goto disable_resources;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun return 0;
234*4882a593Smuzhiyun disable_resources:
235*4882a593Smuzhiyun ahci_platform_disable_resources(hpriv);
236*4882a593Smuzhiyun return rc;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(ahci_da850_pm_ops, ahci_platform_suspend,
240*4882a593Smuzhiyun ahci_platform_resume);
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun static const struct of_device_id ahci_da850_of_match[] = {
243*4882a593Smuzhiyun { .compatible = "ti,da850-ahci", },
244*4882a593Smuzhiyun { },
245*4882a593Smuzhiyun };
246*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ahci_da850_of_match);
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun static struct platform_driver ahci_da850_driver = {
249*4882a593Smuzhiyun .probe = ahci_da850_probe,
250*4882a593Smuzhiyun .remove = ata_platform_remove_one,
251*4882a593Smuzhiyun .driver = {
252*4882a593Smuzhiyun .name = DRV_NAME,
253*4882a593Smuzhiyun .of_match_table = ahci_da850_of_match,
254*4882a593Smuzhiyun .pm = &ahci_da850_pm_ops,
255*4882a593Smuzhiyun },
256*4882a593Smuzhiyun };
257*4882a593Smuzhiyun module_platform_driver(ahci_da850_driver);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun MODULE_DESCRIPTION("DaVinci DA850 AHCI SATA platform driver");
260*4882a593Smuzhiyun MODULE_AUTHOR("Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>");
261*4882a593Smuzhiyun MODULE_LICENSE("GPL");
262