xref: /linux/drivers/pci/controller/dwc/pcie-intel-gw.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCIe host controller driver for Intel Gateway SoCs
4  *
5  * Copyright (c) 2019 Intel Corporation.
6  */
7 
8 #include <linux/bitfield.h>
9 #include <linux/clk.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/iopoll.h>
12 #include <linux/pci_regs.h>
13 #include <linux/phy/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/property.h>
16 #include <linux/reset.h>
17 
18 #include "../../pci.h"
19 #include "pcie-designware.h"
20 
21 #define PORT_AFR_N_FTS_GEN12_DFT	(SZ_128 - 1)
22 #define PORT_AFR_N_FTS_GEN3		180
23 #define PORT_AFR_N_FTS_GEN4		196
24 
25 /* PCIe Application logic Registers */
26 #define PCIE_APP_CCR			0x10
27 #define PCIE_APP_CCR_LTSSM_ENABLE	BIT(0)
28 
29 #define PCIE_APP_MSG_CR			0x30
30 #define PCIE_APP_MSG_XMT_PM_TURNOFF	BIT(0)
31 
32 #define PCIE_APP_PMC			0x44
33 #define PCIE_APP_PMC_IN_L2		BIT(20)
34 
35 #define PCIE_APP_IRNEN			0xF4
36 #define PCIE_APP_IRNCR			0xF8
37 #define PCIE_APP_IRN_AER_REPORT		BIT(0)
38 #define PCIE_APP_IRN_PME		BIT(2)
39 #define PCIE_APP_IRN_RX_VDM_MSG		BIT(4)
40 #define PCIE_APP_IRN_PM_TO_ACK		BIT(9)
41 #define PCIE_APP_IRN_LINK_AUTO_BW_STAT	BIT(11)
42 #define PCIE_APP_IRN_BW_MGT		BIT(12)
43 #define PCIE_APP_IRN_INTA		BIT(13)
44 #define PCIE_APP_IRN_INTB		BIT(14)
45 #define PCIE_APP_IRN_INTC		BIT(15)
46 #define PCIE_APP_IRN_INTD		BIT(16)
47 #define PCIE_APP_IRN_MSG_LTR		BIT(18)
48 #define PCIE_APP_IRN_SYS_ERR_RC		BIT(29)
49 
50 #define PCIE_APP_IRN_INT \
51 	(PCIE_APP_IRN_AER_REPORT | PCIE_APP_IRN_PME | \
52 	PCIE_APP_IRN_RX_VDM_MSG | PCIE_APP_IRN_SYS_ERR_RC | \
53 	PCIE_APP_IRN_PM_TO_ACK | PCIE_APP_IRN_MSG_LTR | \
54 	PCIE_APP_IRN_BW_MGT | PCIE_APP_IRN_LINK_AUTO_BW_STAT | \
55 	PCIE_APP_IRN_INTA | PCIE_APP_IRN_INTB | \
56 	PCIE_APP_IRN_INTC | PCIE_APP_IRN_INTD)
57 
58 #define RESET_INTERVAL_MS		100
59 
60 struct intel_pcie {
61 	struct dw_pcie		pci;
62 	void __iomem		*app_base;
63 	struct gpio_desc	*reset_gpio;
64 	u32			rst_intrvl;
65 	struct clk		*core_clk;
66 	struct reset_control	*core_rst;
67 	struct phy		*phy;
68 };
69 
pcie_update_bits(void __iomem * base,u32 ofs,u32 mask,u32 val)70 static void pcie_update_bits(void __iomem *base, u32 ofs, u32 mask, u32 val)
71 {
72 	u32 old;
73 
74 	old = readl(base + ofs);
75 	val = (old & ~mask) | (val & mask);
76 
77 	if (val != old)
78 		writel(val, base + ofs);
79 }
80 
pcie_app_wr(struct intel_pcie * pcie,u32 ofs,u32 val)81 static inline void pcie_app_wr(struct intel_pcie *pcie, u32 ofs, u32 val)
82 {
83 	writel(val, pcie->app_base + ofs);
84 }
85 
pcie_app_wr_mask(struct intel_pcie * pcie,u32 ofs,u32 mask,u32 val)86 static void pcie_app_wr_mask(struct intel_pcie *pcie, u32 ofs,
87 			     u32 mask, u32 val)
88 {
89 	pcie_update_bits(pcie->app_base, ofs, mask, val);
90 }
91 
pcie_rc_cfg_rd(struct intel_pcie * pcie,u32 ofs)92 static inline u32 pcie_rc_cfg_rd(struct intel_pcie *pcie, u32 ofs)
93 {
94 	return dw_pcie_readl_dbi(&pcie->pci, ofs);
95 }
96 
pcie_rc_cfg_wr(struct intel_pcie * pcie,u32 ofs,u32 val)97 static inline void pcie_rc_cfg_wr(struct intel_pcie *pcie, u32 ofs, u32 val)
98 {
99 	dw_pcie_writel_dbi(&pcie->pci, ofs, val);
100 }
101 
pcie_rc_cfg_wr_mask(struct intel_pcie * pcie,u32 ofs,u32 mask,u32 val)102 static void pcie_rc_cfg_wr_mask(struct intel_pcie *pcie, u32 ofs,
103 				u32 mask, u32 val)
104 {
105 	pcie_update_bits(pcie->pci.dbi_base, ofs, mask, val);
106 }
107 
intel_pcie_ltssm_enable(struct intel_pcie * pcie)108 static void intel_pcie_ltssm_enable(struct intel_pcie *pcie)
109 {
110 	pcie_app_wr_mask(pcie, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE,
111 			 PCIE_APP_CCR_LTSSM_ENABLE);
112 }
113 
intel_pcie_ltssm_disable(struct intel_pcie * pcie)114 static void intel_pcie_ltssm_disable(struct intel_pcie *pcie)
115 {
116 	pcie_app_wr_mask(pcie, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE, 0);
117 }
118 
intel_pcie_link_setup(struct intel_pcie * pcie)119 static void intel_pcie_link_setup(struct intel_pcie *pcie)
120 {
121 	u32 val;
122 	u8 offset = dw_pcie_find_capability(&pcie->pci, PCI_CAP_ID_EXP);
123 
124 	val = pcie_rc_cfg_rd(pcie, offset + PCI_EXP_LNKCTL);
125 
126 	val &= ~(PCI_EXP_LNKCTL_LD | PCI_EXP_LNKCTL_ASPMC);
127 	pcie_rc_cfg_wr(pcie, offset + PCI_EXP_LNKCTL, val);
128 }
129 
intel_pcie_init_n_fts(struct dw_pcie * pci)130 static void intel_pcie_init_n_fts(struct dw_pcie *pci)
131 {
132 	switch (pci->max_link_speed) {
133 	case 3:
134 		pci->n_fts[1] = PORT_AFR_N_FTS_GEN3;
135 		break;
136 	case 4:
137 		pci->n_fts[1] = PORT_AFR_N_FTS_GEN4;
138 		break;
139 	default:
140 		pci->n_fts[1] = PORT_AFR_N_FTS_GEN12_DFT;
141 		break;
142 	}
143 	pci->n_fts[0] = PORT_AFR_N_FTS_GEN12_DFT;
144 }
145 
intel_pcie_ep_rst_init(struct intel_pcie * pcie)146 static int intel_pcie_ep_rst_init(struct intel_pcie *pcie)
147 {
148 	struct device *dev = pcie->pci.dev;
149 	int ret;
150 
151 	pcie->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
152 	if (IS_ERR(pcie->reset_gpio)) {
153 		ret = PTR_ERR(pcie->reset_gpio);
154 		if (ret != -EPROBE_DEFER)
155 			dev_err(dev, "Failed to request PCIe GPIO: %d\n", ret);
156 		return ret;
157 	}
158 
159 	/* Make initial reset last for 100us */
160 	usleep_range(100, 200);
161 
162 	return 0;
163 }
164 
intel_pcie_core_rst_assert(struct intel_pcie * pcie)165 static void intel_pcie_core_rst_assert(struct intel_pcie *pcie)
166 {
167 	reset_control_assert(pcie->core_rst);
168 }
169 
intel_pcie_core_rst_deassert(struct intel_pcie * pcie)170 static void intel_pcie_core_rst_deassert(struct intel_pcie *pcie)
171 {
172 	/*
173 	 * One micro-second delay to make sure the reset pulse
174 	 * wide enough so that core reset is clean.
175 	 */
176 	udelay(1);
177 	reset_control_deassert(pcie->core_rst);
178 
179 	/*
180 	 * Some SoC core reset also reset PHY, more delay needed
181 	 * to make sure the reset process is done.
182 	 */
183 	usleep_range(1000, 2000);
184 }
185 
intel_pcie_device_rst_assert(struct intel_pcie * pcie)186 static void intel_pcie_device_rst_assert(struct intel_pcie *pcie)
187 {
188 	gpiod_set_value_cansleep(pcie->reset_gpio, 1);
189 }
190 
intel_pcie_device_rst_deassert(struct intel_pcie * pcie)191 static void intel_pcie_device_rst_deassert(struct intel_pcie *pcie)
192 {
193 	msleep(pcie->rst_intrvl);
194 	gpiod_set_value_cansleep(pcie->reset_gpio, 0);
195 }
196 
intel_pcie_core_irq_enable(struct intel_pcie * pcie)197 static void intel_pcie_core_irq_enable(struct intel_pcie *pcie)
198 {
199 	pcie_app_wr(pcie, PCIE_APP_IRNEN, 0);
200 	pcie_app_wr(pcie, PCIE_APP_IRNCR, PCIE_APP_IRN_INT);
201 	pcie_app_wr(pcie, PCIE_APP_IRNEN, PCIE_APP_IRN_INT);
202 }
203 
intel_pcie_core_irq_disable(struct intel_pcie * pcie)204 static void intel_pcie_core_irq_disable(struct intel_pcie *pcie)
205 {
206 	pcie_app_wr(pcie, PCIE_APP_IRNEN, 0);
207 	pcie_app_wr(pcie, PCIE_APP_IRNCR, PCIE_APP_IRN_INT);
208 }
209 
intel_pcie_get_resources(struct platform_device * pdev)210 static int intel_pcie_get_resources(struct platform_device *pdev)
211 {
212 	struct intel_pcie *pcie = platform_get_drvdata(pdev);
213 	struct dw_pcie *pci = &pcie->pci;
214 	struct device *dev = pci->dev;
215 	int ret;
216 
217 	pcie->core_clk = devm_clk_get(dev, NULL);
218 	if (IS_ERR(pcie->core_clk)) {
219 		ret = PTR_ERR(pcie->core_clk);
220 		if (ret != -EPROBE_DEFER)
221 			dev_err(dev, "Failed to get clks: %d\n", ret);
222 		return ret;
223 	}
224 
225 	pcie->core_rst = devm_reset_control_get(dev, NULL);
226 	if (IS_ERR(pcie->core_rst)) {
227 		ret = PTR_ERR(pcie->core_rst);
228 		if (ret != -EPROBE_DEFER)
229 			dev_err(dev, "Failed to get resets: %d\n", ret);
230 		return ret;
231 	}
232 
233 	ret = device_property_read_u32(dev, "reset-assert-ms",
234 				       &pcie->rst_intrvl);
235 	if (ret)
236 		pcie->rst_intrvl = RESET_INTERVAL_MS;
237 
238 	pcie->app_base = devm_platform_ioremap_resource_byname(pdev, "app");
239 	if (IS_ERR(pcie->app_base))
240 		return PTR_ERR(pcie->app_base);
241 
242 	pcie->phy = devm_phy_get(dev, "pcie");
243 	if (IS_ERR(pcie->phy)) {
244 		ret = PTR_ERR(pcie->phy);
245 		if (ret != -EPROBE_DEFER)
246 			dev_err(dev, "Couldn't get pcie-phy: %d\n", ret);
247 		return ret;
248 	}
249 
250 	return 0;
251 }
252 
intel_pcie_wait_l2(struct intel_pcie * pcie)253 static int intel_pcie_wait_l2(struct intel_pcie *pcie)
254 {
255 	u32 value;
256 	int ret;
257 	struct dw_pcie *pci = &pcie->pci;
258 
259 	if (pci->max_link_speed < 3)
260 		return 0;
261 
262 	/* Send PME_TURN_OFF message */
263 	pcie_app_wr_mask(pcie, PCIE_APP_MSG_CR, PCIE_APP_MSG_XMT_PM_TURNOFF,
264 			 PCIE_APP_MSG_XMT_PM_TURNOFF);
265 
266 	/* Read PMC status and wait for falling into L2 link state */
267 	ret = readl_poll_timeout(pcie->app_base + PCIE_APP_PMC, value,
268 				 value & PCIE_APP_PMC_IN_L2, 20,
269 				 jiffies_to_usecs(5 * HZ));
270 	if (ret)
271 		dev_err(pcie->pci.dev, "PCIe link enter L2 timeout!\n");
272 
273 	return ret;
274 }
275 
intel_pcie_turn_off(struct intel_pcie * pcie)276 static void intel_pcie_turn_off(struct intel_pcie *pcie)
277 {
278 	if (dw_pcie_link_up(&pcie->pci))
279 		intel_pcie_wait_l2(pcie);
280 
281 	/* Put endpoint device in reset state */
282 	intel_pcie_device_rst_assert(pcie);
283 	pcie_rc_cfg_wr_mask(pcie, PCI_COMMAND, PCI_COMMAND_MEMORY, 0);
284 }
285 
intel_pcie_start_link(struct dw_pcie * pci)286 static int intel_pcie_start_link(struct dw_pcie *pci)
287 {
288 	struct intel_pcie *pcie = dev_get_drvdata(pci->dev);
289 
290 	intel_pcie_device_rst_deassert(pcie);
291 	intel_pcie_ltssm_enable(pcie);
292 
293 	return 0;
294 }
295 
intel_pcie_host_setup(struct intel_pcie * pcie)296 static int intel_pcie_host_setup(struct intel_pcie *pcie)
297 {
298 	int ret;
299 	struct dw_pcie *pci = &pcie->pci;
300 
301 	intel_pcie_core_rst_assert(pcie);
302 	intel_pcie_device_rst_assert(pcie);
303 	intel_pcie_core_rst_deassert(pcie);
304 
305 	/* Controller clock must be provided earlier than PHY */
306 	ret = clk_prepare_enable(pcie->core_clk);
307 	if (ret) {
308 		dev_err(pcie->pci.dev, "Core clock enable failed: %d\n", ret);
309 		goto clk_err;
310 	}
311 
312 	ret = phy_init(pcie->phy);
313 	if (ret)
314 		goto phy_err;
315 
316 	intel_pcie_ltssm_disable(pcie);
317 	intel_pcie_link_setup(pcie);
318 	intel_pcie_init_n_fts(pci);
319 
320 	dw_pcie_upconfig_setup(pci);
321 
322 	intel_pcie_core_irq_enable(pcie);
323 
324 	return 0;
325 
326 phy_err:
327 	clk_disable_unprepare(pcie->core_clk);
328 clk_err:
329 	intel_pcie_core_rst_assert(pcie);
330 
331 	return ret;
332 }
333 
__intel_pcie_remove(struct intel_pcie * pcie)334 static void __intel_pcie_remove(struct intel_pcie *pcie)
335 {
336 	intel_pcie_core_irq_disable(pcie);
337 	intel_pcie_turn_off(pcie);
338 	clk_disable_unprepare(pcie->core_clk);
339 	intel_pcie_core_rst_assert(pcie);
340 	phy_exit(pcie->phy);
341 }
342 
intel_pcie_remove(struct platform_device * pdev)343 static void intel_pcie_remove(struct platform_device *pdev)
344 {
345 	struct intel_pcie *pcie = platform_get_drvdata(pdev);
346 	struct dw_pcie_rp *pp = &pcie->pci.pp;
347 
348 	dw_pcie_host_deinit(pp);
349 	__intel_pcie_remove(pcie);
350 }
351 
intel_pcie_suspend_noirq(struct device * dev)352 static int intel_pcie_suspend_noirq(struct device *dev)
353 {
354 	struct intel_pcie *pcie = dev_get_drvdata(dev);
355 	int ret;
356 
357 	intel_pcie_core_irq_disable(pcie);
358 	ret = intel_pcie_wait_l2(pcie);
359 	if (ret)
360 		return ret;
361 
362 	phy_exit(pcie->phy);
363 	clk_disable_unprepare(pcie->core_clk);
364 	return ret;
365 }
366 
intel_pcie_resume_noirq(struct device * dev)367 static int intel_pcie_resume_noirq(struct device *dev)
368 {
369 	struct intel_pcie *pcie = dev_get_drvdata(dev);
370 
371 	return intel_pcie_host_setup(pcie);
372 }
373 
intel_pcie_rc_init(struct dw_pcie_rp * pp)374 static int intel_pcie_rc_init(struct dw_pcie_rp *pp)
375 {
376 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
377 	struct intel_pcie *pcie = dev_get_drvdata(pci->dev);
378 
379 	return intel_pcie_host_setup(pcie);
380 }
381 
382 static const struct dw_pcie_ops intel_pcie_ops = {
383 	.start_link = intel_pcie_start_link,
384 };
385 
386 static const struct dw_pcie_host_ops intel_pcie_dw_ops = {
387 	.init = intel_pcie_rc_init,
388 };
389 
intel_pcie_probe(struct platform_device * pdev)390 static int intel_pcie_probe(struct platform_device *pdev)
391 {
392 	struct device *dev = &pdev->dev;
393 	struct intel_pcie *pcie;
394 	struct dw_pcie_rp *pp;
395 	struct resource *res;
396 	struct dw_pcie *pci;
397 	int ret;
398 
399 	pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
400 	if (!pcie)
401 		return -ENOMEM;
402 
403 	platform_set_drvdata(pdev, pcie);
404 	pci = &pcie->pci;
405 	pci->dev = dev;
406 	pci->use_parent_dt_ranges = true;
407 	pp = &pci->pp;
408 
409 	ret = intel_pcie_get_resources(pdev);
410 	if (ret)
411 		return ret;
412 
413 	ret = intel_pcie_ep_rst_init(pcie);
414 	if (ret)
415 		return ret;
416 
417 	pci->ops = &intel_pcie_ops;
418 	pp->ops = &intel_pcie_dw_ops;
419 
420 	/*
421 	 * If the 'atu' region is not available in the devicetree, use the
422 	 * default offset from DBI region for backwards compatibility. The
423 	 * 'atu' region should always be specified in the devicetree, as
424 	 * this is a hardware-specific address that should not be defined
425 	 * in the driver.
426 	 */
427 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "atu");
428 	if (!res) {
429 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
430 		pci->dbi_base = devm_pci_remap_cfg_resource(pci->dev, res);
431 		if (IS_ERR(pci->dbi_base))
432 			return PTR_ERR(pci->dbi_base);
433 
434 		pci->dbi_phys_addr = res->start;
435 		pci->atu_base = devm_ioremap(dev, res->start + 0xC0000, SZ_4K);
436 		if (!pci->atu_base) {
437 			dev_err(dev, "failed to remap ATU space\n");
438 			return -ENOMEM;
439 		}
440 
441 		pci->atu_size = SZ_4K;
442 		pci->atu_phys_addr = res->start + 0xC0000;
443 		dev_warn(dev, "ATU region not specified in DT. Using default offset\n");
444 	}
445 
446 	ret = dw_pcie_host_init(pp);
447 	if (ret) {
448 		dev_err(dev, "Cannot initialize host\n");
449 		return ret;
450 	}
451 
452 	return 0;
453 }
454 
455 static const struct dev_pm_ops intel_pcie_pm_ops = {
456 	NOIRQ_SYSTEM_SLEEP_PM_OPS(intel_pcie_suspend_noirq,
457 				  intel_pcie_resume_noirq)
458 };
459 
460 static const struct of_device_id of_intel_pcie_match[] = {
461 	{ .compatible = "intel,lgm-pcie" },
462 	{}
463 };
464 
465 static struct platform_driver intel_pcie_driver = {
466 	.probe = intel_pcie_probe,
467 	.remove = intel_pcie_remove,
468 	.driver = {
469 		.name = "intel-gw-pcie",
470 		.of_match_table = of_intel_pcie_match,
471 		.pm = &intel_pcie_pm_ops,
472 	},
473 };
474 builtin_platform_driver(intel_pcie_driver);
475