PCI: dwc: Move link handling into common code
[linux-2.6-microblaze.git] / drivers / pci / controller / dwc / pci-exynos.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCIe host controller driver for Samsung Exynos SoCs
4  *
5  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6  *              https://www.samsung.com
7  *
8  * Author: Jingoo Han <jg1.han@samsung.com>
9  */
10
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/gpio.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/of_device.h>
18 #include <linux/of_gpio.h>
19 #include <linux/pci.h>
20 #include <linux/platform_device.h>
21 #include <linux/phy/phy.h>
22 #include <linux/resource.h>
23 #include <linux/signal.h>
24 #include <linux/types.h>
25
26 #include "pcie-designware.h"
27
28 #define to_exynos_pcie(x)       dev_get_drvdata((x)->dev)
29
30 /* PCIe ELBI registers */
31 #define PCIE_IRQ_PULSE                  0x000
32 #define IRQ_INTA_ASSERT                 BIT(0)
33 #define IRQ_INTB_ASSERT                 BIT(2)
34 #define IRQ_INTC_ASSERT                 BIT(4)
35 #define IRQ_INTD_ASSERT                 BIT(6)
36 #define PCIE_IRQ_LEVEL                  0x004
37 #define PCIE_IRQ_SPECIAL                0x008
38 #define PCIE_IRQ_EN_PULSE               0x00c
39 #define PCIE_IRQ_EN_LEVEL               0x010
40 #define IRQ_MSI_ENABLE                  BIT(2)
41 #define PCIE_IRQ_EN_SPECIAL             0x014
42 #define PCIE_PWR_RESET                  0x018
43 #define PCIE_CORE_RESET                 0x01c
44 #define PCIE_CORE_RESET_ENABLE          BIT(0)
45 #define PCIE_STICKY_RESET               0x020
46 #define PCIE_NONSTICKY_RESET            0x024
47 #define PCIE_APP_INIT_RESET             0x028
48 #define PCIE_APP_LTSSM_ENABLE           0x02c
49 #define PCIE_ELBI_RDLH_LINKUP           0x064
50 #define PCIE_ELBI_LTSSM_ENABLE          0x1
51 #define PCIE_ELBI_SLV_AWMISC            0x11c
52 #define PCIE_ELBI_SLV_ARMISC            0x120
53 #define PCIE_ELBI_SLV_DBI_ENABLE        BIT(21)
54
55 struct exynos_pcie_mem_res {
56         void __iomem *elbi_base;   /* DT 0th resource: PCIe CTRL */
57 };
58
59 struct exynos_pcie_clk_res {
60         struct clk *clk;
61         struct clk *bus_clk;
62 };
63
64 struct exynos_pcie {
65         struct dw_pcie                  *pci;
66         struct exynos_pcie_mem_res      *mem_res;
67         struct exynos_pcie_clk_res      *clk_res;
68         const struct exynos_pcie_ops    *ops;
69         int                             reset_gpio;
70
71         struct phy                      *phy;
72 };
73
74 struct exynos_pcie_ops {
75         int (*get_mem_resources)(struct platform_device *pdev,
76                         struct exynos_pcie *ep);
77         int (*get_clk_resources)(struct exynos_pcie *ep);
78         int (*init_clk_resources)(struct exynos_pcie *ep);
79         void (*deinit_clk_resources)(struct exynos_pcie *ep);
80 };
81
82 static int exynos5440_pcie_get_mem_resources(struct platform_device *pdev,
83                                              struct exynos_pcie *ep)
84 {
85         struct dw_pcie *pci = ep->pci;
86         struct device *dev = pci->dev;
87
88         ep->mem_res = devm_kzalloc(dev, sizeof(*ep->mem_res), GFP_KERNEL);
89         if (!ep->mem_res)
90                 return -ENOMEM;
91
92         ep->mem_res->elbi_base = devm_platform_ioremap_resource(pdev, 0);
93         if (IS_ERR(ep->mem_res->elbi_base))
94                 return PTR_ERR(ep->mem_res->elbi_base);
95
96         return 0;
97 }
98
99 static int exynos5440_pcie_get_clk_resources(struct exynos_pcie *ep)
100 {
101         struct dw_pcie *pci = ep->pci;
102         struct device *dev = pci->dev;
103
104         ep->clk_res = devm_kzalloc(dev, sizeof(*ep->clk_res), GFP_KERNEL);
105         if (!ep->clk_res)
106                 return -ENOMEM;
107
108         ep->clk_res->clk = devm_clk_get(dev, "pcie");
109         if (IS_ERR(ep->clk_res->clk)) {
110                 dev_err(dev, "Failed to get pcie rc clock\n");
111                 return PTR_ERR(ep->clk_res->clk);
112         }
113
114         ep->clk_res->bus_clk = devm_clk_get(dev, "pcie_bus");
115         if (IS_ERR(ep->clk_res->bus_clk)) {
116                 dev_err(dev, "Failed to get pcie bus clock\n");
117                 return PTR_ERR(ep->clk_res->bus_clk);
118         }
119
120         return 0;
121 }
122
123 static int exynos5440_pcie_init_clk_resources(struct exynos_pcie *ep)
124 {
125         struct dw_pcie *pci = ep->pci;
126         struct device *dev = pci->dev;
127         int ret;
128
129         ret = clk_prepare_enable(ep->clk_res->clk);
130         if (ret) {
131                 dev_err(dev, "cannot enable pcie rc clock");
132                 return ret;
133         }
134
135         ret = clk_prepare_enable(ep->clk_res->bus_clk);
136         if (ret) {
137                 dev_err(dev, "cannot enable pcie bus clock");
138                 goto err_bus_clk;
139         }
140
141         return 0;
142
143 err_bus_clk:
144         clk_disable_unprepare(ep->clk_res->clk);
145
146         return ret;
147 }
148
149 static void exynos5440_pcie_deinit_clk_resources(struct exynos_pcie *ep)
150 {
151         clk_disable_unprepare(ep->clk_res->bus_clk);
152         clk_disable_unprepare(ep->clk_res->clk);
153 }
154
155 static const struct exynos_pcie_ops exynos5440_pcie_ops = {
156         .get_mem_resources      = exynos5440_pcie_get_mem_resources,
157         .get_clk_resources      = exynos5440_pcie_get_clk_resources,
158         .init_clk_resources     = exynos5440_pcie_init_clk_resources,
159         .deinit_clk_resources   = exynos5440_pcie_deinit_clk_resources,
160 };
161
162 static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
163 {
164         writel(val, base + reg);
165 }
166
167 static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
168 {
169         return readl(base + reg);
170 }
171
172 static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
173 {
174         u32 val;
175
176         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_AWMISC);
177         if (on)
178                 val |= PCIE_ELBI_SLV_DBI_ENABLE;
179         else
180                 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
181         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
182 }
183
184 static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
185 {
186         u32 val;
187
188         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_ARMISC);
189         if (on)
190                 val |= PCIE_ELBI_SLV_DBI_ENABLE;
191         else
192                 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
193         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
194 }
195
196 static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
197 {
198         u32 val;
199
200         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
201         val &= ~PCIE_CORE_RESET_ENABLE;
202         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
203         exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_PWR_RESET);
204         exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_STICKY_RESET);
205         exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_NONSTICKY_RESET);
206 }
207
208 static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
209 {
210         u32 val;
211
212         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
213         val |= PCIE_CORE_RESET_ENABLE;
214
215         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
216         exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_STICKY_RESET);
217         exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_NONSTICKY_RESET);
218         exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_APP_INIT_RESET);
219         exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_APP_INIT_RESET);
220 }
221
222 static void exynos_pcie_assert_reset(struct exynos_pcie *ep)
223 {
224         struct dw_pcie *pci = ep->pci;
225         struct device *dev = pci->dev;
226
227         if (ep->reset_gpio >= 0)
228                 devm_gpio_request_one(dev, ep->reset_gpio,
229                                 GPIOF_OUT_INIT_HIGH, "RESET");
230 }
231
232 static int exynos_pcie_start_link(struct dw_pcie *pci)
233 {
234         struct exynos_pcie *ep = to_exynos_pcie(pci);
235
236         /* assert LTSSM enable */
237         exynos_pcie_writel(ep->mem_res->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
238                           PCIE_APP_LTSSM_ENABLE);
239
240         /* check if the link is up or not */
241         if (!dw_pcie_wait_for_link(pci))
242                 return 0;
243
244         phy_power_off(ep->phy);
245         return -ETIMEDOUT;
246 }
247
248 static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
249 {
250         u32 val;
251
252         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_PULSE);
253         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_PULSE);
254 }
255
256 static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
257 {
258         u32 val;
259
260         /* enable INTX interrupt */
261         val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
262                 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
263         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_PULSE);
264 }
265
266 static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
267 {
268         struct exynos_pcie *ep = arg;
269
270         exynos_pcie_clear_irq_pulse(ep);
271         return IRQ_HANDLED;
272 }
273
274 static void exynos_pcie_msi_init(struct exynos_pcie *ep)
275 {
276         struct dw_pcie *pci = ep->pci;
277         struct pcie_port *pp = &pci->pp;
278         u32 val;
279
280         dw_pcie_msi_init(pp);
281
282         /* enable MSI interrupt */
283         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_EN_LEVEL);
284         val |= IRQ_MSI_ENABLE;
285         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_LEVEL);
286 }
287
288 static void exynos_pcie_enable_interrupts(struct exynos_pcie *ep)
289 {
290         exynos_pcie_enable_irq_pulse(ep);
291
292         if (IS_ENABLED(CONFIG_PCI_MSI))
293                 exynos_pcie_msi_init(ep);
294 }
295
296 static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
297                                 u32 reg, size_t size)
298 {
299         struct exynos_pcie *ep = to_exynos_pcie(pci);
300         u32 val;
301
302         exynos_pcie_sideband_dbi_r_mode(ep, true);
303         dw_pcie_read(base + reg, size, &val);
304         exynos_pcie_sideband_dbi_r_mode(ep, false);
305         return val;
306 }
307
308 static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
309                                   u32 reg, size_t size, u32 val)
310 {
311         struct exynos_pcie *ep = to_exynos_pcie(pci);
312
313         exynos_pcie_sideband_dbi_w_mode(ep, true);
314         dw_pcie_write(base + reg, size, val);
315         exynos_pcie_sideband_dbi_w_mode(ep, false);
316 }
317
318 static int exynos_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
319                                    int where, int size, u32 *val)
320 {
321         struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
322
323         if (PCI_SLOT(devfn)) {
324                 *val = ~0;
325                 return PCIBIOS_DEVICE_NOT_FOUND;
326         }
327
328         *val = dw_pcie_read_dbi(pci, where, size);
329         return PCIBIOS_SUCCESSFUL;
330 }
331
332 static int exynos_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
333                                    int where, int size, u32 val)
334 {
335         struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
336
337         if (PCI_SLOT(devfn))
338                 return PCIBIOS_DEVICE_NOT_FOUND;
339
340         dw_pcie_write_dbi(pci, where, size, val);
341         return PCIBIOS_SUCCESSFUL;
342 }
343
344 static struct pci_ops exynos_pci_ops = {
345         .read = exynos_pcie_rd_own_conf,
346         .write = exynos_pcie_wr_own_conf,
347 };
348
349 static int exynos_pcie_link_up(struct dw_pcie *pci)
350 {
351         struct exynos_pcie *ep = to_exynos_pcie(pci);
352         u32 val;
353
354         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_RDLH_LINKUP);
355         if (val == PCIE_ELBI_LTSSM_ENABLE)
356                 return 1;
357
358         return 0;
359 }
360
361 static int exynos_pcie_host_init(struct pcie_port *pp)
362 {
363         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
364         struct exynos_pcie *ep = to_exynos_pcie(pci);
365
366         pp->bridge->ops = &exynos_pci_ops;
367
368         exynos_pcie_assert_core_reset(ep);
369
370         phy_reset(ep->phy);
371
372         exynos_pcie_writel(ep->mem_res->elbi_base, 1,
373                         PCIE_PWR_RESET);
374
375         phy_power_on(ep->phy);
376         phy_init(ep->phy);
377
378         exynos_pcie_deassert_core_reset(ep);
379         dw_pcie_setup_rc(pp);
380         exynos_pcie_assert_reset(ep);
381
382         exynos_pcie_enable_interrupts(ep);
383
384         return 0;
385 }
386
387 static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
388         .host_init = exynos_pcie_host_init,
389 };
390
391 static int __init exynos_add_pcie_port(struct exynos_pcie *ep,
392                                        struct platform_device *pdev)
393 {
394         struct dw_pcie *pci = ep->pci;
395         struct pcie_port *pp = &pci->pp;
396         struct device *dev = &pdev->dev;
397         int ret;
398
399         pp->irq = platform_get_irq(pdev, 1);
400         if (pp->irq < 0)
401                 return pp->irq;
402
403         ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
404                                 IRQF_SHARED, "exynos-pcie", ep);
405         if (ret) {
406                 dev_err(dev, "failed to request irq\n");
407                 return ret;
408         }
409
410         pp->ops = &exynos_pcie_host_ops;
411
412         ret = dw_pcie_host_init(pp);
413         if (ret) {
414                 dev_err(dev, "failed to initialize host\n");
415                 return ret;
416         }
417
418         return 0;
419 }
420
421 static const struct dw_pcie_ops dw_pcie_ops = {
422         .read_dbi = exynos_pcie_read_dbi,
423         .write_dbi = exynos_pcie_write_dbi,
424         .link_up = exynos_pcie_link_up,
425         .start_link = exynos_pcie_start_link,
426 };
427
428 static int __init exynos_pcie_probe(struct platform_device *pdev)
429 {
430         struct device *dev = &pdev->dev;
431         struct dw_pcie *pci;
432         struct exynos_pcie *ep;
433         struct device_node *np = dev->of_node;
434         int ret;
435
436         ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
437         if (!ep)
438                 return -ENOMEM;
439
440         pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
441         if (!pci)
442                 return -ENOMEM;
443
444         pci->dev = dev;
445         pci->ops = &dw_pcie_ops;
446
447         ep->pci = pci;
448         ep->ops = (const struct exynos_pcie_ops *)
449                 of_device_get_match_data(dev);
450
451         ep->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
452
453         ep->phy = devm_of_phy_get(dev, np, NULL);
454         if (IS_ERR(ep->phy)) {
455                 if (PTR_ERR(ep->phy) != -ENODEV)
456                         return PTR_ERR(ep->phy);
457
458                 ep->phy = NULL;
459         }
460
461         if (ep->ops && ep->ops->get_mem_resources) {
462                 ret = ep->ops->get_mem_resources(pdev, ep);
463                 if (ret)
464                         return ret;
465         }
466
467         if (ep->ops && ep->ops->get_clk_resources &&
468                         ep->ops->init_clk_resources) {
469                 ret = ep->ops->get_clk_resources(ep);
470                 if (ret)
471                         return ret;
472                 ret = ep->ops->init_clk_resources(ep);
473                 if (ret)
474                         return ret;
475         }
476
477         platform_set_drvdata(pdev, ep);
478
479         ret = exynos_add_pcie_port(ep, pdev);
480         if (ret < 0)
481                 goto fail_probe;
482
483         return 0;
484
485 fail_probe:
486         phy_exit(ep->phy);
487
488         if (ep->ops && ep->ops->deinit_clk_resources)
489                 ep->ops->deinit_clk_resources(ep);
490         return ret;
491 }
492
493 static int __exit exynos_pcie_remove(struct platform_device *pdev)
494 {
495         struct exynos_pcie *ep = platform_get_drvdata(pdev);
496
497         if (ep->ops && ep->ops->deinit_clk_resources)
498                 ep->ops->deinit_clk_resources(ep);
499
500         return 0;
501 }
502
503 static const struct of_device_id exynos_pcie_of_match[] = {
504         {
505                 .compatible = "samsung,exynos5440-pcie",
506                 .data = &exynos5440_pcie_ops
507         },
508         {},
509 };
510
511 static struct platform_driver exynos_pcie_driver = {
512         .remove         = __exit_p(exynos_pcie_remove),
513         .driver = {
514                 .name   = "exynos-pcie",
515                 .of_match_table = exynos_pcie_of_match,
516         },
517 };
518
519 /* Exynos PCIe driver does not allow module unload */
520
521 static int __init exynos_pcie_init(void)
522 {
523         return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
524 }
525 subsys_initcall(exynos_pcie_init);