1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Marvell Armada-8K SoCs
5 * Armada-8K PCIe Glue Layer Source Code
7 * Copyright (C) 2016 Marvell Technology Group Ltd.
9 * Author: Yehuda Yitshak <yehuday@marvell.com>
10 * Author: Shadi Ammouri <shadi@marvell.com>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
19 #include <linux/pci.h>
20 #include <linux/phy/phy.h>
21 #include <linux/platform_device.h>
22 #include <linux/resource.h>
23 #include <linux/of_pci.h>
25 #include "pcie-designware.h"
27 #define ARMADA8K_PCIE_MAX_LANES PCIE_LNK_X4
29 struct armada8k_pcie
{
33 struct phy
*phy
[ARMADA8K_PCIE_MAX_LANES
];
34 unsigned int phy_count
;
37 #define PCIE_VENDOR_REGS_OFFSET 0x8000
39 #define PCIE_GLOBAL_CONTROL_REG (PCIE_VENDOR_REGS_OFFSET + 0x0)
40 #define PCIE_APP_LTSSM_EN BIT(2)
41 #define PCIE_DEVICE_TYPE_SHIFT 4
42 #define PCIE_DEVICE_TYPE_MASK 0xF
43 #define PCIE_DEVICE_TYPE_RC 0x4 /* Root complex */
45 #define PCIE_GLOBAL_STATUS_REG (PCIE_VENDOR_REGS_OFFSET + 0x8)
46 #define PCIE_GLB_STS_RDLH_LINK_UP BIT(1)
47 #define PCIE_GLB_STS_PHY_LINK_UP BIT(9)
49 #define PCIE_GLOBAL_INT_CAUSE1_REG (PCIE_VENDOR_REGS_OFFSET + 0x1C)
50 #define PCIE_GLOBAL_INT_MASK1_REG (PCIE_VENDOR_REGS_OFFSET + 0x20)
51 #define PCIE_INT_A_ASSERT_MASK BIT(9)
52 #define PCIE_INT_B_ASSERT_MASK BIT(10)
53 #define PCIE_INT_C_ASSERT_MASK BIT(11)
54 #define PCIE_INT_D_ASSERT_MASK BIT(12)
56 #define PCIE_ARCACHE_TRC_REG (PCIE_VENDOR_REGS_OFFSET + 0x50)
57 #define PCIE_AWCACHE_TRC_REG (PCIE_VENDOR_REGS_OFFSET + 0x54)
58 #define PCIE_ARUSER_REG (PCIE_VENDOR_REGS_OFFSET + 0x5C)
59 #define PCIE_AWUSER_REG (PCIE_VENDOR_REGS_OFFSET + 0x60)
61 * AR/AW Cache defaults: Normal memory, Write-Back, Read / Write
64 #define ARCACHE_DEFAULT_VALUE 0x3511
65 #define AWCACHE_DEFAULT_VALUE 0x5311
67 #define DOMAIN_OUTER_SHAREABLE 0x2
68 #define AX_USER_DOMAIN_MASK 0x3
69 #define AX_USER_DOMAIN_SHIFT 4
71 #define to_armada8k_pcie(x) dev_get_drvdata((x)->dev)
73 static void armada8k_pcie_disable_phys(struct armada8k_pcie
*pcie
)
77 for (i
= 0; i
< ARMADA8K_PCIE_MAX_LANES
; i
++) {
78 phy_power_off(pcie
->phy
[i
]);
79 phy_exit(pcie
->phy
[i
]);
83 static int armada8k_pcie_enable_phys(struct armada8k_pcie
*pcie
)
88 for (i
= 0; i
< ARMADA8K_PCIE_MAX_LANES
; i
++) {
89 ret
= phy_init(pcie
->phy
[i
]);
93 ret
= phy_set_mode_ext(pcie
->phy
[i
], PHY_MODE_PCIE
,
96 phy_exit(pcie
->phy
[i
]);
100 ret
= phy_power_on(pcie
->phy
[i
]);
102 phy_exit(pcie
->phy
[i
]);
110 static int armada8k_pcie_setup_phys(struct armada8k_pcie
*pcie
)
112 struct dw_pcie
*pci
= pcie
->pci
;
113 struct device
*dev
= pci
->dev
;
114 struct device_node
*node
= dev
->of_node
;
118 for (i
= 0; i
< ARMADA8K_PCIE_MAX_LANES
; i
++) {
119 pcie
->phy
[i
] = devm_of_phy_get_by_index(dev
, node
, i
);
120 if (IS_ERR(pcie
->phy
[i
])) {
121 if (PTR_ERR(pcie
->phy
[i
]) != -ENODEV
)
122 return PTR_ERR(pcie
->phy
[i
]);
131 /* Old bindings miss the PHY handle, so just warn if there is no PHY */
132 if (!pcie
->phy_count
)
133 dev_warn(dev
, "No available PHY\n");
135 ret
= armada8k_pcie_enable_phys(pcie
);
137 dev_err(dev
, "Failed to initialize PHY(s) (%d)\n", ret
);
142 static int armada8k_pcie_link_up(struct dw_pcie
*pci
)
145 u32 mask
= PCIE_GLB_STS_RDLH_LINK_UP
| PCIE_GLB_STS_PHY_LINK_UP
;
147 reg
= dw_pcie_readl_dbi(pci
, PCIE_GLOBAL_STATUS_REG
);
149 if ((reg
& mask
) == mask
)
152 dev_dbg(pci
->dev
, "No link detected (Global-Status: 0x%08x).\n", reg
);
156 static int armada8k_pcie_start_link(struct dw_pcie
*pci
)
161 reg
= dw_pcie_readl_dbi(pci
, PCIE_GLOBAL_CONTROL_REG
);
162 reg
|= PCIE_APP_LTSSM_EN
;
163 dw_pcie_writel_dbi(pci
, PCIE_GLOBAL_CONTROL_REG
, reg
);
168 static int armada8k_pcie_host_init(struct dw_pcie_rp
*pp
)
171 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
173 if (!dw_pcie_link_up(pci
)) {
174 /* Disable LTSSM state machine to enable configuration */
175 reg
= dw_pcie_readl_dbi(pci
, PCIE_GLOBAL_CONTROL_REG
);
176 reg
&= ~(PCIE_APP_LTSSM_EN
);
177 dw_pcie_writel_dbi(pci
, PCIE_GLOBAL_CONTROL_REG
, reg
);
180 /* Set the device to root complex mode */
181 reg
= dw_pcie_readl_dbi(pci
, PCIE_GLOBAL_CONTROL_REG
);
182 reg
&= ~(PCIE_DEVICE_TYPE_MASK
<< PCIE_DEVICE_TYPE_SHIFT
);
183 reg
|= PCIE_DEVICE_TYPE_RC
<< PCIE_DEVICE_TYPE_SHIFT
;
184 dw_pcie_writel_dbi(pci
, PCIE_GLOBAL_CONTROL_REG
, reg
);
186 /* Set the PCIe master AxCache attributes */
187 dw_pcie_writel_dbi(pci
, PCIE_ARCACHE_TRC_REG
, ARCACHE_DEFAULT_VALUE
);
188 dw_pcie_writel_dbi(pci
, PCIE_AWCACHE_TRC_REG
, AWCACHE_DEFAULT_VALUE
);
190 /* Set the PCIe master AxDomain attributes */
191 reg
= dw_pcie_readl_dbi(pci
, PCIE_ARUSER_REG
);
192 reg
&= ~(AX_USER_DOMAIN_MASK
<< AX_USER_DOMAIN_SHIFT
);
193 reg
|= DOMAIN_OUTER_SHAREABLE
<< AX_USER_DOMAIN_SHIFT
;
194 dw_pcie_writel_dbi(pci
, PCIE_ARUSER_REG
, reg
);
196 reg
= dw_pcie_readl_dbi(pci
, PCIE_AWUSER_REG
);
197 reg
&= ~(AX_USER_DOMAIN_MASK
<< AX_USER_DOMAIN_SHIFT
);
198 reg
|= DOMAIN_OUTER_SHAREABLE
<< AX_USER_DOMAIN_SHIFT
;
199 dw_pcie_writel_dbi(pci
, PCIE_AWUSER_REG
, reg
);
201 /* Enable INT A-D interrupts */
202 reg
= dw_pcie_readl_dbi(pci
, PCIE_GLOBAL_INT_MASK1_REG
);
203 reg
|= PCIE_INT_A_ASSERT_MASK
| PCIE_INT_B_ASSERT_MASK
|
204 PCIE_INT_C_ASSERT_MASK
| PCIE_INT_D_ASSERT_MASK
;
205 dw_pcie_writel_dbi(pci
, PCIE_GLOBAL_INT_MASK1_REG
, reg
);
210 static irqreturn_t
armada8k_pcie_irq_handler(int irq
, void *arg
)
212 struct armada8k_pcie
*pcie
= arg
;
213 struct dw_pcie
*pci
= pcie
->pci
;
217 * Interrupts are directly handled by the device driver of the
218 * PCI device. However, they are also latched into the PCIe
219 * controller, so we simply discard them.
221 val
= dw_pcie_readl_dbi(pci
, PCIE_GLOBAL_INT_CAUSE1_REG
);
222 dw_pcie_writel_dbi(pci
, PCIE_GLOBAL_INT_CAUSE1_REG
, val
);
227 static const struct dw_pcie_host_ops armada8k_pcie_host_ops
= {
228 .init
= armada8k_pcie_host_init
,
231 static int armada8k_add_pcie_port(struct armada8k_pcie
*pcie
,
232 struct platform_device
*pdev
)
234 struct dw_pcie
*pci
= pcie
->pci
;
235 struct dw_pcie_rp
*pp
= &pci
->pp
;
236 struct device
*dev
= &pdev
->dev
;
239 pp
->ops
= &armada8k_pcie_host_ops
;
241 pp
->irq
= platform_get_irq(pdev
, 0);
245 ret
= devm_request_irq(dev
, pp
->irq
, armada8k_pcie_irq_handler
,
246 IRQF_SHARED
, "armada8k-pcie", pcie
);
248 dev_err(dev
, "failed to request irq %d\n", pp
->irq
);
252 ret
= dw_pcie_host_init(pp
);
254 dev_err(dev
, "failed to initialize host: %d\n", ret
);
261 static const struct dw_pcie_ops dw_pcie_ops
= {
262 .link_up
= armada8k_pcie_link_up
,
263 .start_link
= armada8k_pcie_start_link
,
266 static int armada8k_pcie_probe(struct platform_device
*pdev
)
269 struct armada8k_pcie
*pcie
;
270 struct device
*dev
= &pdev
->dev
;
271 struct resource
*base
;
274 pcie
= devm_kzalloc(dev
, sizeof(*pcie
), GFP_KERNEL
);
278 pci
= devm_kzalloc(dev
, sizeof(*pci
), GFP_KERNEL
);
283 pci
->ops
= &dw_pcie_ops
;
287 pcie
->clk
= devm_clk_get(dev
, NULL
);
288 if (IS_ERR(pcie
->clk
))
289 return PTR_ERR(pcie
->clk
);
291 ret
= clk_prepare_enable(pcie
->clk
);
295 pcie
->clk_reg
= devm_clk_get(dev
, "reg");
296 if (pcie
->clk_reg
== ERR_PTR(-EPROBE_DEFER
)) {
300 if (!IS_ERR(pcie
->clk_reg
)) {
301 ret
= clk_prepare_enable(pcie
->clk_reg
);
306 /* Get the dw-pcie unit configuration/control registers base. */
307 base
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "ctrl");
308 pci
->dbi_base
= devm_pci_remap_cfg_resource(dev
, base
);
309 if (IS_ERR(pci
->dbi_base
)) {
310 ret
= PTR_ERR(pci
->dbi_base
);
314 ret
= armada8k_pcie_setup_phys(pcie
);
318 platform_set_drvdata(pdev
, pcie
);
320 ret
= armada8k_add_pcie_port(pcie
, pdev
);
327 armada8k_pcie_disable_phys(pcie
);
329 clk_disable_unprepare(pcie
->clk_reg
);
331 clk_disable_unprepare(pcie
->clk
);
336 static const struct of_device_id armada8k_pcie_of_match
[] = {
337 { .compatible
= "marvell,armada8k-pcie", },
341 static struct platform_driver armada8k_pcie_driver
= {
342 .probe
= armada8k_pcie_probe
,
344 .name
= "armada8k-pcie",
345 .of_match_table
= armada8k_pcie_of_match
,
346 .suppress_bind_attrs
= true,
349 builtin_platform_driver(armada8k_pcie_driver
);