• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * PCIe host controller driver for Marvell Armada-8K SoCs
3  *
4  * Armada-8K PCIe Glue Layer Source Code
5  *
6  * Copyright (C) 2016 Marvell Technology Group Ltd.
7  *
8  * Author: Yehuda Yitshak <yehuday@marvell.com>
9  * Author: Shadi Ammouri <shadi@marvell.com>
10  *
11  * This file is licensed under the terms of the GNU General Public
12  * License version 2. This program is licensed "as is" without any
13  * warranty of any kind, whether express or implied.
14  */
15 
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/of.h>
22 #include <linux/pci.h>
23 #include <linux/phy/phy.h>
24 #include <linux/platform_device.h>
25 #include <linux/resource.h>
26 #include <linux/of_pci.h>
27 #include <linux/of_irq.h>
28 
29 #include "pcie-designware.h"
30 
31 struct armada8k_pcie {
32 	struct pcie_port pp;		/* pp.dbi_base is DT ctrl */
33 	struct clk *clk;
34 };
35 
36 #define PCIE_VENDOR_REGS_OFFSET		0x8000
37 
38 #define PCIE_GLOBAL_CONTROL_REG		(PCIE_VENDOR_REGS_OFFSET + 0x0)
39 #define PCIE_APP_LTSSM_EN		BIT(2)
40 #define PCIE_DEVICE_TYPE_SHIFT		4
41 #define PCIE_DEVICE_TYPE_MASK		0xF
42 #define PCIE_DEVICE_TYPE_RC		0x4 /* Root complex */
43 
44 #define PCIE_GLOBAL_STATUS_REG		(PCIE_VENDOR_REGS_OFFSET + 0x8)
45 #define PCIE_GLB_STS_RDLH_LINK_UP	BIT(1)
46 #define PCIE_GLB_STS_PHY_LINK_UP	BIT(9)
47 
48 #define PCIE_GLOBAL_INT_CAUSE1_REG	(PCIE_VENDOR_REGS_OFFSET + 0x1C)
49 #define PCIE_GLOBAL_INT_MASK1_REG	(PCIE_VENDOR_REGS_OFFSET + 0x20)
50 #define PCIE_INT_A_ASSERT_MASK		BIT(9)
51 #define PCIE_INT_B_ASSERT_MASK		BIT(10)
52 #define PCIE_INT_C_ASSERT_MASK		BIT(11)
53 #define PCIE_INT_D_ASSERT_MASK		BIT(12)
54 
55 #define PCIE_ARCACHE_TRC_REG		(PCIE_VENDOR_REGS_OFFSET + 0x50)
56 #define PCIE_AWCACHE_TRC_REG		(PCIE_VENDOR_REGS_OFFSET + 0x54)
57 #define PCIE_ARUSER_REG			(PCIE_VENDOR_REGS_OFFSET + 0x5C)
58 #define PCIE_AWUSER_REG			(PCIE_VENDOR_REGS_OFFSET + 0x60)
59 /*
60  * AR/AW Cache defauls: Normal memory, Write-Back, Read / Write
61  * allocate
62  */
63 #define ARCACHE_DEFAULT_VALUE		0x3511
64 #define AWCACHE_DEFAULT_VALUE		0x5311
65 
66 #define DOMAIN_OUTER_SHAREABLE		0x2
67 #define AX_USER_DOMAIN_MASK		0x3
68 #define AX_USER_DOMAIN_SHIFT		4
69 
70 #define to_armada8k_pcie(x)	container_of(x, struct armada8k_pcie, pp)
71 
armada8k_pcie_link_up(struct pcie_port * pp)72 static int armada8k_pcie_link_up(struct pcie_port *pp)
73 {
74 	u32 reg;
75 	u32 mask = PCIE_GLB_STS_RDLH_LINK_UP | PCIE_GLB_STS_PHY_LINK_UP;
76 
77 	reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_STATUS_REG);
78 
79 	if ((reg & mask) == mask)
80 		return 1;
81 
82 	dev_dbg(pp->dev, "No link detected (Global-Status: 0x%08x).\n", reg);
83 	return 0;
84 }
85 
armada8k_pcie_establish_link(struct armada8k_pcie * pcie)86 static void armada8k_pcie_establish_link(struct armada8k_pcie *pcie)
87 {
88 	struct pcie_port *pp = &pcie->pp;
89 	u32 reg;
90 
91 	if (!dw_pcie_link_up(pp)) {
92 		/* Disable LTSSM state machine to enable configuration */
93 		reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
94 		reg &= ~(PCIE_APP_LTSSM_EN);
95 		dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
96 	}
97 
98 	/* Set the device to root complex mode */
99 	reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
100 	reg &= ~(PCIE_DEVICE_TYPE_MASK << PCIE_DEVICE_TYPE_SHIFT);
101 	reg |= PCIE_DEVICE_TYPE_RC << PCIE_DEVICE_TYPE_SHIFT;
102 	dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
103 
104 	/* Set the PCIe master AxCache attributes */
105 	dw_pcie_writel_rc(pp, PCIE_ARCACHE_TRC_REG, ARCACHE_DEFAULT_VALUE);
106 	dw_pcie_writel_rc(pp, PCIE_AWCACHE_TRC_REG, AWCACHE_DEFAULT_VALUE);
107 
108 	/* Set the PCIe master AxDomain attributes */
109 	reg = dw_pcie_readl_rc(pp, PCIE_ARUSER_REG);
110 	reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT);
111 	reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT;
112 	dw_pcie_writel_rc(pp, PCIE_ARUSER_REG, reg);
113 
114 	reg = dw_pcie_readl_rc(pp, PCIE_AWUSER_REG);
115 	reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT);
116 	reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT;
117 	dw_pcie_writel_rc(pp, PCIE_AWUSER_REG, reg);
118 
119 	/* Enable INT A-D interrupts */
120 	reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_INT_MASK1_REG);
121 	reg |= PCIE_INT_A_ASSERT_MASK | PCIE_INT_B_ASSERT_MASK |
122 	       PCIE_INT_C_ASSERT_MASK | PCIE_INT_D_ASSERT_MASK;
123 	dw_pcie_writel_rc(pp, PCIE_GLOBAL_INT_MASK1_REG, reg);
124 
125 	if (!dw_pcie_link_up(pp)) {
126 		/* Configuration done. Start LTSSM */
127 		reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
128 		reg |= PCIE_APP_LTSSM_EN;
129 		dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
130 	}
131 
132 	/* Wait until the link becomes active again */
133 	if (dw_pcie_wait_for_link(pp))
134 		dev_err(pp->dev, "Link not up after reconfiguration\n");
135 }
136 
armada8k_pcie_host_init(struct pcie_port * pp)137 static void armada8k_pcie_host_init(struct pcie_port *pp)
138 {
139 	struct armada8k_pcie *pcie = to_armada8k_pcie(pp);
140 
141 	dw_pcie_setup_rc(pp);
142 	armada8k_pcie_establish_link(pcie);
143 }
144 
armada8k_pcie_irq_handler(int irq,void * arg)145 static irqreturn_t armada8k_pcie_irq_handler(int irq, void *arg)
146 {
147 	struct armada8k_pcie *pcie = arg;
148 	struct pcie_port *pp = &pcie->pp;
149 	u32 val;
150 
151 	/*
152 	 * Interrupts are directly handled by the device driver of the
153 	 * PCI device. However, they are also latched into the PCIe
154 	 * controller, so we simply discard them.
155 	 */
156 	val = dw_pcie_readl_rc(pp, PCIE_GLOBAL_INT_CAUSE1_REG);
157 	dw_pcie_writel_rc(pp, PCIE_GLOBAL_INT_CAUSE1_REG, val);
158 
159 	return IRQ_HANDLED;
160 }
161 
162 static struct pcie_host_ops armada8k_pcie_host_ops = {
163 	.link_up = armada8k_pcie_link_up,
164 	.host_init = armada8k_pcie_host_init,
165 };
166 
armada8k_add_pcie_port(struct armada8k_pcie * pcie,struct platform_device * pdev)167 static int armada8k_add_pcie_port(struct armada8k_pcie *pcie,
168 				  struct platform_device *pdev)
169 {
170 	struct pcie_port *pp = &pcie->pp;
171 	struct device *dev = &pdev->dev;
172 	int ret;
173 
174 	pp->root_bus_nr = -1;
175 	pp->ops = &armada8k_pcie_host_ops;
176 
177 	pp->irq = platform_get_irq(pdev, 0);
178 	if (!pp->irq) {
179 		dev_err(dev, "failed to get irq for port\n");
180 		return -ENODEV;
181 	}
182 
183 	ret = devm_request_irq(dev, pp->irq, armada8k_pcie_irq_handler,
184 			       IRQF_SHARED, "armada8k-pcie", pcie);
185 	if (ret) {
186 		dev_err(dev, "failed to request irq %d\n", pp->irq);
187 		return ret;
188 	}
189 
190 	ret = dw_pcie_host_init(pp);
191 	if (ret) {
192 		dev_err(dev, "failed to initialize host: %d\n", ret);
193 		return ret;
194 	}
195 
196 	return 0;
197 }
198 
armada8k_pcie_probe(struct platform_device * pdev)199 static int armada8k_pcie_probe(struct platform_device *pdev)
200 {
201 	struct armada8k_pcie *pcie;
202 	struct pcie_port *pp;
203 	struct device *dev = &pdev->dev;
204 	struct resource *base;
205 	int ret;
206 
207 	pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
208 	if (!pcie)
209 		return -ENOMEM;
210 
211 	pcie->clk = devm_clk_get(dev, NULL);
212 	if (IS_ERR(pcie->clk))
213 		return PTR_ERR(pcie->clk);
214 
215 	clk_prepare_enable(pcie->clk);
216 
217 	pp = &pcie->pp;
218 	pp->dev = dev;
219 
220 	/* Get the dw-pcie unit configuration/control registers base. */
221 	base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl");
222 	pp->dbi_base = devm_ioremap_resource(dev, base);
223 	if (IS_ERR(pp->dbi_base)) {
224 		dev_err(dev, "couldn't remap regs base %p\n", base);
225 		ret = PTR_ERR(pp->dbi_base);
226 		goto fail;
227 	}
228 
229 	ret = armada8k_add_pcie_port(pcie, pdev);
230 	if (ret)
231 		goto fail;
232 
233 	return 0;
234 
235 fail:
236 	if (!IS_ERR(pcie->clk))
237 		clk_disable_unprepare(pcie->clk);
238 
239 	return ret;
240 }
241 
242 static const struct of_device_id armada8k_pcie_of_match[] = {
243 	{ .compatible = "marvell,armada8k-pcie", },
244 	{},
245 };
246 
247 static struct platform_driver armada8k_pcie_driver = {
248 	.probe		= armada8k_pcie_probe,
249 	.driver = {
250 		.name	= "armada8k-pcie",
251 		.of_match_table = of_match_ptr(armada8k_pcie_of_match),
252 	},
253 };
254 builtin_platform_driver(armada8k_pcie_driver);
255