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