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