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