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