]> Git Repo - J-linux.git/blob - drivers/pci/controller/dwc/pcie-intel-gw.c
Merge branch 'for-5.14/google' into for-linus
[J-linux.git] / drivers / pci / controller / dwc / pcie-intel-gw.c
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
59 struct intel_pcie_soc {
60         unsigned int    pcie_ver;
61 };
62
63 struct 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
73 static 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
84 static inline void pcie_app_wr(struct intel_pcie_port *lpp, u32 ofs, u32 val)
85 {
86         writel(val, lpp->app_base + ofs);
87 }
88
89 static void pcie_app_wr_mask(struct intel_pcie_port *lpp, u32 ofs,
90                              u32 mask, u32 val)
91 {
92         pcie_update_bits(lpp->app_base, ofs, mask, val);
93 }
94
95 static inline u32 pcie_rc_cfg_rd(struct intel_pcie_port *lpp, u32 ofs)
96 {
97         return dw_pcie_readl_dbi(&lpp->pci, ofs);
98 }
99
100 static inline void pcie_rc_cfg_wr(struct intel_pcie_port *lpp, u32 ofs, u32 val)
101 {
102         dw_pcie_writel_dbi(&lpp->pci, ofs, val);
103 }
104
105 static void pcie_rc_cfg_wr_mask(struct intel_pcie_port *lpp, u32 ofs,
106                                 u32 mask, u32 val)
107 {
108         pcie_update_bits(lpp->pci.dbi_base, ofs, mask, val);
109 }
110
111 static void intel_pcie_ltssm_enable(struct intel_pcie_port *lpp)
112 {
113         pcie_app_wr_mask(lpp, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE,
114                          PCIE_APP_CCR_LTSSM_ENABLE);
115 }
116
117 static void intel_pcie_ltssm_disable(struct intel_pcie_port *lpp)
118 {
119         pcie_app_wr_mask(lpp, PCIE_APP_CCR, PCIE_APP_CCR_LTSSM_ENABLE, 0);
120 }
121
122 static void intel_pcie_link_setup(struct intel_pcie_port *lpp)
123 {
124         u32 val;
125         u8 offset = dw_pcie_find_capability(&lpp->pci, PCI_CAP_ID_EXP);
126
127         val = pcie_rc_cfg_rd(lpp, offset + PCI_EXP_LNKCTL);
128
129         val &= ~(PCI_EXP_LNKCTL_LD | PCI_EXP_LNKCTL_ASPMC);
130         pcie_rc_cfg_wr(lpp, offset + PCI_EXP_LNKCTL, val);
131 }
132
133 static 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
149 static int intel_pcie_ep_rst_init(struct intel_pcie_port *lpp)
150 {
151         struct device *dev = lpp->pci.dev;
152         int ret;
153
154         lpp->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
155         if (IS_ERR(lpp->reset_gpio)) {
156                 ret = PTR_ERR(lpp->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
168 static void intel_pcie_core_rst_assert(struct intel_pcie_port *lpp)
169 {
170         reset_control_assert(lpp->core_rst);
171 }
172
173 static void intel_pcie_core_rst_deassert(struct intel_pcie_port *lpp)
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(lpp->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
189 static void intel_pcie_device_rst_assert(struct intel_pcie_port *lpp)
190 {
191         gpiod_set_value_cansleep(lpp->reset_gpio, 1);
192 }
193
194 static void intel_pcie_device_rst_deassert(struct intel_pcie_port *lpp)
195 {
196         msleep(lpp->rst_intrvl);
197         gpiod_set_value_cansleep(lpp->reset_gpio, 0);
198 }
199
200 static void intel_pcie_core_irq_disable(struct intel_pcie_port *lpp)
201 {
202         pcie_app_wr(lpp, PCIE_APP_IRNEN, 0);
203         pcie_app_wr(lpp, PCIE_APP_IRNCR, PCIE_APP_IRN_INT);
204 }
205
206 static int intel_pcie_get_resources(struct platform_device *pdev)
207 {
208         struct intel_pcie_port *lpp = platform_get_drvdata(pdev);
209         struct dw_pcie *pci = &lpp->pci;
210         struct device *dev = pci->dev;
211         int ret;
212
213         lpp->core_clk = devm_clk_get(dev, NULL);
214         if (IS_ERR(lpp->core_clk)) {
215                 ret = PTR_ERR(lpp->core_clk);
216                 if (ret != -EPROBE_DEFER)
217                         dev_err(dev, "Failed to get clks: %d\n", ret);
218                 return ret;
219         }
220
221         lpp->core_rst = devm_reset_control_get(dev, NULL);
222         if (IS_ERR(lpp->core_rst)) {
223                 ret = PTR_ERR(lpp->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                                        &lpp->rst_intrvl);
231         if (ret)
232                 lpp->rst_intrvl = RESET_INTERVAL_MS;
233
234         lpp->app_base = devm_platform_ioremap_resource_byname(pdev, "app");
235         if (IS_ERR(lpp->app_base))
236                 return PTR_ERR(lpp->app_base);
237
238         lpp->phy = devm_phy_get(dev, "pcie");
239         if (IS_ERR(lpp->phy)) {
240                 ret = PTR_ERR(lpp->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
249 static int intel_pcie_wait_l2(struct intel_pcie_port *lpp)
250 {
251         u32 value;
252         int ret;
253         struct dw_pcie *pci = &lpp->pci;
254
255         if (pci->link_gen < 3)
256                 return 0;
257
258         /* Send PME_TURN_OFF message */
259         pcie_app_wr_mask(lpp, 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(lpp->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(lpp->pci.dev, "PCIe link enter L2 timeout!\n");
268
269         return ret;
270 }
271
272 static void intel_pcie_turn_off(struct intel_pcie_port *lpp)
273 {
274         if (dw_pcie_link_up(&lpp->pci))
275                 intel_pcie_wait_l2(lpp);
276
277         /* Put endpoint device in reset state */
278         intel_pcie_device_rst_assert(lpp);
279         pcie_rc_cfg_wr_mask(lpp, PCI_COMMAND, PCI_COMMAND_MEMORY, 0);
280 }
281
282 static int intel_pcie_host_setup(struct intel_pcie_port *lpp)
283 {
284         int ret;
285         struct dw_pcie *pci = &lpp->pci;
286
287         intel_pcie_core_rst_assert(lpp);
288         intel_pcie_device_rst_assert(lpp);
289
290         ret = phy_init(lpp->phy);
291         if (ret)
292                 return ret;
293
294         intel_pcie_core_rst_deassert(lpp);
295
296         ret = clk_prepare_enable(lpp->core_clk);
297         if (ret) {
298                 dev_err(lpp->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(lpp);
305         intel_pcie_link_setup(lpp);
306         intel_pcie_init_n_fts(pci);
307         dw_pcie_setup_rc(&pci->pp);
308         dw_pcie_upconfig_setup(pci);
309
310         intel_pcie_device_rst_deassert(lpp);
311         intel_pcie_ltssm_enable(lpp);
312
313         ret = dw_pcie_wait_for_link(pci);
314         if (ret)
315                 goto app_init_err;
316
317         /* Enable integrated interrupts */
318         pcie_app_wr_mask(lpp, PCIE_APP_IRNEN, PCIE_APP_IRN_INT,
319                          PCIE_APP_IRN_INT);
320
321         return 0;
322
323 app_init_err:
324         clk_disable_unprepare(lpp->core_clk);
325 clk_err:
326         intel_pcie_core_rst_assert(lpp);
327         phy_exit(lpp->phy);
328
329         return ret;
330 }
331
332 static void __intel_pcie_remove(struct intel_pcie_port *lpp)
333 {
334         intel_pcie_core_irq_disable(lpp);
335         intel_pcie_turn_off(lpp);
336         clk_disable_unprepare(lpp->core_clk);
337         intel_pcie_core_rst_assert(lpp);
338         phy_exit(lpp->phy);
339 }
340
341 static int intel_pcie_remove(struct platform_device *pdev)
342 {
343         struct intel_pcie_port *lpp = platform_get_drvdata(pdev);
344         struct pcie_port *pp = &lpp->pci.pp;
345
346         dw_pcie_host_deinit(pp);
347         __intel_pcie_remove(lpp);
348
349         return 0;
350 }
351
352 static int __maybe_unused intel_pcie_suspend_noirq(struct device *dev)
353 {
354         struct intel_pcie_port *lpp = dev_get_drvdata(dev);
355         int ret;
356
357         intel_pcie_core_irq_disable(lpp);
358         ret = intel_pcie_wait_l2(lpp);
359         if (ret)
360                 return ret;
361
362         phy_exit(lpp->phy);
363         clk_disable_unprepare(lpp->core_clk);
364         return ret;
365 }
366
367 static int __maybe_unused intel_pcie_resume_noirq(struct device *dev)
368 {
369         struct intel_pcie_port *lpp = dev_get_drvdata(dev);
370
371         return intel_pcie_host_setup(lpp);
372 }
373
374 static int intel_pcie_rc_init(struct pcie_port *pp)
375 {
376         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
377         struct intel_pcie_port *lpp = dev_get_drvdata(pci->dev);
378
379         return intel_pcie_host_setup(lpp);
380 }
381
382 static u64 intel_pcie_cpu_addr(struct dw_pcie *pcie, u64 cpu_addr)
383 {
384         return cpu_addr + BUS_IATU_OFFSET;
385 }
386
387 static const struct dw_pcie_ops intel_pcie_ops = {
388         .cpu_addr_fixup = intel_pcie_cpu_addr,
389 };
390
391 static const struct dw_pcie_host_ops intel_pcie_dw_ops = {
392         .host_init =            intel_pcie_rc_init,
393 };
394
395 static const struct intel_pcie_soc pcie_data = {
396         .pcie_ver =             0x520A,
397 };
398
399 static int intel_pcie_probe(struct platform_device *pdev)
400 {
401         const struct intel_pcie_soc *data;
402         struct device *dev = &pdev->dev;
403         struct intel_pcie_port *lpp;
404         struct pcie_port *pp;
405         struct dw_pcie *pci;
406         int ret;
407
408         lpp = devm_kzalloc(dev, sizeof(*lpp), GFP_KERNEL);
409         if (!lpp)
410                 return -ENOMEM;
411
412         platform_set_drvdata(pdev, lpp);
413         pci = &lpp->pci;
414         pci->dev = dev;
415         pp = &pci->pp;
416
417         ret = intel_pcie_get_resources(pdev);
418         if (ret)
419                 return ret;
420
421         ret = intel_pcie_ep_rst_init(lpp);
422         if (ret)
423                 return ret;
424
425         data = device_get_match_data(dev);
426         if (!data)
427                 return -ENODEV;
428
429         pci->ops = &intel_pcie_ops;
430         pci->version = data->pcie_ver;
431         pp->ops = &intel_pcie_dw_ops;
432
433         ret = dw_pcie_host_init(pp);
434         if (ret) {
435                 dev_err(dev, "Cannot initialize host\n");
436                 return ret;
437         }
438
439         return 0;
440 }
441
442 static const struct dev_pm_ops intel_pcie_pm_ops = {
443         SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(intel_pcie_suspend_noirq,
444                                       intel_pcie_resume_noirq)
445 };
446
447 static const struct of_device_id of_intel_pcie_match[] = {
448         { .compatible = "intel,lgm-pcie", .data = &pcie_data },
449         {}
450 };
451
452 static struct platform_driver intel_pcie_driver = {
453         .probe = intel_pcie_probe,
454         .remove = intel_pcie_remove,
455         .driver = {
456                 .name = "intel-gw-pcie",
457                 .of_match_table = of_intel_pcie_match,
458                 .pm = &intel_pcie_pm_ops,
459         },
460 };
461 builtin_platform_driver(intel_pcie_driver);
This page took 0.05606 seconds and 4 git commands to generate.