1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DaVinci DA850 AHCI SATA platform driver
6 #include <linux/kernel.h>
7 #include <linux/module.h>
9 #include <linux/device.h>
10 #include <linux/platform_device.h>
11 #include <linux/libata.h>
12 #include <linux/ahci_platform.h>
15 #define DRV_NAME "ahci_da850"
16 #define HARDRESET_RETRIES 5
18 /* SATA PHY Control Register offset from AHCI base */
19 #define SATA_P0PHYCR_REG 0x178
21 #define SATA_PHY_MPY(x) ((x) << 0)
22 #define SATA_PHY_LOS(x) ((x) << 6)
23 #define SATA_PHY_RXCDR(x) ((x) << 10)
24 #define SATA_PHY_RXEQ(x) ((x) << 13)
25 #define SATA_PHY_TXSWING(x) ((x) << 19)
26 #define SATA_PHY_ENPLL(x) ((x) << 31)
28 static void da850_sata_init(struct device
*dev
, void __iomem
*pwrdn_reg
,
29 void __iomem
*ahci_base
, u32 mpy
)
33 /* Enable SATA clock receiver */
34 val
= readl(pwrdn_reg
);
36 writel(val
, pwrdn_reg
);
38 val
= SATA_PHY_MPY(mpy
) | SATA_PHY_LOS(1) | SATA_PHY_RXCDR(4) |
39 SATA_PHY_RXEQ(1) | SATA_PHY_TXSWING(3) | SATA_PHY_ENPLL(1);
41 writel(val
, ahci_base
+ SATA_P0PHYCR_REG
);
44 static u32
ahci_da850_calculate_mpy(unsigned long refclk_rate
)
46 u32 pll_output
= 1500000000, needed
;
49 * We need to determine the value of the multiplier (MPY) bits.
50 * In order to include the 12.5 multiplier we need to first divide
51 * the refclk rate by ten.
53 * __div64_32() turned out to be unreliable, sometimes returning
56 WARN((refclk_rate
% 10) != 0, "refclk must be divisible by 10");
57 needed
= pll_output
/ (refclk_rate
/ 10);
60 * What we have now is (multiplier * 10).
62 * Let's determine the actual register value we need to write.
86 * We should have divided evenly - if not, return an invalid
93 static int ahci_da850_softreset(struct ata_link
*link
,
94 unsigned int *class, unsigned long deadline
)
98 pmp
= sata_srst_pmp(link
);
101 * There's an issue with the SATA controller on da850 SoCs: if we
102 * enable Port Multiplier support, but the drive is connected directly
103 * to the board, it can't be detected. As a workaround: if PMP is
104 * enabled, we first call ahci_do_softreset() and pass it the result of
105 * sata_srst_pmp(). If this call fails, we retry with pmp = 0.
107 ret
= ahci_do_softreset(link
, class, pmp
, deadline
, ahci_check_ready
);
108 if (pmp
&& ret
== -EBUSY
)
109 return ahci_do_softreset(link
, class, 0,
110 deadline
, ahci_check_ready
);
115 static int ahci_da850_hardreset(struct ata_link
*link
,
116 unsigned int *class, unsigned long deadline
)
118 int ret
, retry
= HARDRESET_RETRIES
;
122 * In order to correctly service the LCD controller of the da850 SoC,
123 * we increased the PLL0 frequency to 456MHz from the default 300MHz.
125 * This made the SATA controller unstable and the hardreset operation
126 * does not always succeed the first time. Before really giving up to
127 * bring up the link, retry the reset a couple times.
130 ret
= ahci_do_hardreset(link
, class, deadline
, &online
);
138 static struct ata_port_operations ahci_da850_port_ops
= {
139 .inherits
= &ahci_platform_ops
,
140 .softreset
= ahci_da850_softreset
,
142 * No need to override .pmp_softreset - it's only used for actual
145 .hardreset
= ahci_da850_hardreset
,
146 .pmp_hardreset
= ahci_da850_hardreset
,
149 static const struct ata_port_info ahci_da850_port_info
= {
150 .flags
= AHCI_FLAG_COMMON
,
151 .pio_mask
= ATA_PIO4
,
152 .udma_mask
= ATA_UDMA6
,
153 .port_ops
= &ahci_da850_port_ops
,
156 static struct scsi_host_template ahci_platform_sht
= {
160 static int ahci_da850_probe(struct platform_device
*pdev
)
162 struct device
*dev
= &pdev
->dev
;
163 struct ahci_host_priv
*hpriv
;
164 void __iomem
*pwrdn_reg
;
165 struct resource
*res
;
170 hpriv
= ahci_platform_get_resources(pdev
, 0);
172 return PTR_ERR(hpriv
);
175 * Internally ahci_platform_get_resources() calls clk_get(dev, NULL)
176 * when trying to obtain the functional clock. This SATA controller
177 * uses two clocks for which we specify two connection ids. If we don't
178 * have the functional clock at this point - call clk_get() again with
181 if (!hpriv
->clks
[0]) {
182 clk
= clk_get(dev
, "fck");
186 hpriv
->clks
[0] = clk
;
190 * The second clock used by ahci-da850 is the external REFCLK. If we
191 * didn't get it from ahci_platform_get_resources(), let's try to
192 * specify the con_id in clk_get().
194 if (!hpriv
->clks
[1]) {
195 clk
= clk_get(dev
, "refclk");
197 dev_err(dev
, "unable to obtain the reference clock");
201 hpriv
->clks
[1] = clk
;
204 mpy
= ahci_da850_calculate_mpy(clk_get_rate(hpriv
->clks
[1]));
206 dev_err(dev
, "invalid REFCLK multiplier value: 0x%x", mpy
);
210 rc
= ahci_platform_enable_resources(hpriv
);
214 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
217 goto disable_resources
;
220 pwrdn_reg
= devm_ioremap(dev
, res
->start
, resource_size(res
));
223 goto disable_resources
;
226 da850_sata_init(dev
, pwrdn_reg
, hpriv
->mmio
, mpy
);
228 rc
= ahci_platform_init_host(pdev
, hpriv
, &ahci_da850_port_info
,
231 goto disable_resources
;
235 ahci_platform_disable_resources(hpriv
);
239 static SIMPLE_DEV_PM_OPS(ahci_da850_pm_ops
, ahci_platform_suspend
,
240 ahci_platform_resume
);
242 static const struct of_device_id ahci_da850_of_match
[] = {
243 { .compatible
= "ti,da850-ahci", },
246 MODULE_DEVICE_TABLE(of
, ahci_da850_of_match
);
248 static struct platform_driver ahci_da850_driver
= {
249 .probe
= ahci_da850_probe
,
250 .remove
= ata_platform_remove_one
,
253 .of_match_table
= ahci_da850_of_match
,
254 .pm
= &ahci_da850_pm_ops
,
257 module_platform_driver(ahci_da850_driver
);
259 MODULE_DESCRIPTION("DaVinci DA850 AHCI SATA platform driver");
260 MODULE_AUTHOR("Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>");
261 MODULE_LICENSE("GPL");