1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Samsung Exynos SoCs
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6 * https://www.samsung.com
8 * Author: Jingoo Han <jg1.han@samsung.com>
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>
26 #include "pcie-designware.h"
28 #define to_exynos_pcie(x) dev_get_drvdata((x)->dev)
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)
55 struct exynos_pcie_mem_res {
56 void __iomem *elbi_base; /* DT 0th resource: PCIe CTRL */
59 struct exynos_pcie_clk_res {
66 struct exynos_pcie_mem_res *mem_res;
67 struct exynos_pcie_clk_res *clk_res;
68 const struct exynos_pcie_ops *ops;
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);
82 static int exynos5440_pcie_get_mem_resources(struct platform_device *pdev,
83 struct exynos_pcie *ep)
85 struct dw_pcie *pci = ep->pci;
86 struct device *dev = pci->dev;
88 ep->mem_res = devm_kzalloc(dev, sizeof(*ep->mem_res), GFP_KERNEL);
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);
99 static int exynos5440_pcie_get_clk_resources(struct exynos_pcie *ep)
101 struct dw_pcie *pci = ep->pci;
102 struct device *dev = pci->dev;
104 ep->clk_res = devm_kzalloc(dev, sizeof(*ep->clk_res), GFP_KERNEL);
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);
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);
123 static int exynos5440_pcie_init_clk_resources(struct exynos_pcie *ep)
125 struct dw_pcie *pci = ep->pci;
126 struct device *dev = pci->dev;
129 ret = clk_prepare_enable(ep->clk_res->clk);
131 dev_err(dev, "cannot enable pcie rc clock");
135 ret = clk_prepare_enable(ep->clk_res->bus_clk);
137 dev_err(dev, "cannot enable pcie bus clock");
144 clk_disable_unprepare(ep->clk_res->clk);
149 static void exynos5440_pcie_deinit_clk_resources(struct exynos_pcie *ep)
151 clk_disable_unprepare(ep->clk_res->bus_clk);
152 clk_disable_unprepare(ep->clk_res->clk);
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,
162 static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
164 writel(val, base + reg);
167 static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
169 return readl(base + reg);
172 static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
176 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_AWMISC);
178 val |= PCIE_ELBI_SLV_DBI_ENABLE;
180 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
181 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
184 static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
188 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_ARMISC);
190 val |= PCIE_ELBI_SLV_DBI_ENABLE;
192 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
193 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
196 static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
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);
208 static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
212 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
213 val |= PCIE_CORE_RESET_ENABLE;
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);
222 static void exynos_pcie_assert_reset(struct exynos_pcie *ep)
224 struct dw_pcie *pci = ep->pci;
225 struct device *dev = pci->dev;
227 if (ep->reset_gpio >= 0)
228 devm_gpio_request_one(dev, ep->reset_gpio,
229 GPIOF_OUT_INIT_HIGH, "RESET");
232 static int exynos_pcie_start_link(struct dw_pcie *pci)
234 struct exynos_pcie *ep = to_exynos_pcie(pci);
236 /* assert LTSSM enable */
237 exynos_pcie_writel(ep->mem_res->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
238 PCIE_APP_LTSSM_ENABLE);
240 /* check if the link is up or not */
241 if (!dw_pcie_wait_for_link(pci))
244 phy_power_off(ep->phy);
248 static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
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);
256 static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
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);
266 static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
268 struct exynos_pcie *ep = arg;
270 exynos_pcie_clear_irq_pulse(ep);
274 static void exynos_pcie_msi_init(struct exynos_pcie *ep)
278 /* enable MSI interrupt */
279 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_EN_LEVEL);
280 val |= IRQ_MSI_ENABLE;
281 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_LEVEL);
284 static void exynos_pcie_enable_interrupts(struct exynos_pcie *ep)
286 exynos_pcie_enable_irq_pulse(ep);
288 if (IS_ENABLED(CONFIG_PCI_MSI))
289 exynos_pcie_msi_init(ep);
292 static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
293 u32 reg, size_t size)
295 struct exynos_pcie *ep = to_exynos_pcie(pci);
298 exynos_pcie_sideband_dbi_r_mode(ep, true);
299 dw_pcie_read(base + reg, size, &val);
300 exynos_pcie_sideband_dbi_r_mode(ep, false);
304 static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
305 u32 reg, size_t size, u32 val)
307 struct exynos_pcie *ep = to_exynos_pcie(pci);
309 exynos_pcie_sideband_dbi_w_mode(ep, true);
310 dw_pcie_write(base + reg, size, val);
311 exynos_pcie_sideband_dbi_w_mode(ep, false);
314 static int exynos_pcie_rd_own_conf(struct pci_bus *bus, unsigned int devfn,
315 int where, int size, u32 *val)
317 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
319 if (PCI_SLOT(devfn)) {
321 return PCIBIOS_DEVICE_NOT_FOUND;
324 *val = dw_pcie_read_dbi(pci, where, size);
325 return PCIBIOS_SUCCESSFUL;
328 static int exynos_pcie_wr_own_conf(struct pci_bus *bus, unsigned int devfn,
329 int where, int size, u32 val)
331 struct dw_pcie *pci = to_dw_pcie_from_pp(bus->sysdata);
334 return PCIBIOS_DEVICE_NOT_FOUND;
336 dw_pcie_write_dbi(pci, where, size, val);
337 return PCIBIOS_SUCCESSFUL;
340 static struct pci_ops exynos_pci_ops = {
341 .read = exynos_pcie_rd_own_conf,
342 .write = exynos_pcie_wr_own_conf,
345 static int exynos_pcie_link_up(struct dw_pcie *pci)
347 struct exynos_pcie *ep = to_exynos_pcie(pci);
350 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_RDLH_LINKUP);
351 if (val == PCIE_ELBI_LTSSM_ENABLE)
357 static int exynos_pcie_host_init(struct pcie_port *pp)
359 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
360 struct exynos_pcie *ep = to_exynos_pcie(pci);
362 pp->bridge->ops = &exynos_pci_ops;
364 exynos_pcie_assert_core_reset(ep);
368 exynos_pcie_writel(ep->mem_res->elbi_base, 1,
371 phy_power_on(ep->phy);
374 exynos_pcie_deassert_core_reset(ep);
375 exynos_pcie_assert_reset(ep);
377 exynos_pcie_enable_interrupts(ep);
382 static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
383 .host_init = exynos_pcie_host_init,
386 static int __init exynos_add_pcie_port(struct exynos_pcie *ep,
387 struct platform_device *pdev)
389 struct dw_pcie *pci = ep->pci;
390 struct pcie_port *pp = &pci->pp;
391 struct device *dev = &pdev->dev;
394 pp->irq = platform_get_irq(pdev, 1);
398 ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
399 IRQF_SHARED, "exynos-pcie", ep);
401 dev_err(dev, "failed to request irq\n");
405 pp->ops = &exynos_pcie_host_ops;
407 ret = dw_pcie_host_init(pp);
409 dev_err(dev, "failed to initialize host\n");
416 static const struct dw_pcie_ops dw_pcie_ops = {
417 .read_dbi = exynos_pcie_read_dbi,
418 .write_dbi = exynos_pcie_write_dbi,
419 .link_up = exynos_pcie_link_up,
420 .start_link = exynos_pcie_start_link,
423 static int __init exynos_pcie_probe(struct platform_device *pdev)
425 struct device *dev = &pdev->dev;
427 struct exynos_pcie *ep;
428 struct device_node *np = dev->of_node;
431 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
435 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
440 pci->ops = &dw_pcie_ops;
443 ep->ops = (const struct exynos_pcie_ops *)
444 of_device_get_match_data(dev);
446 ep->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
448 ep->phy = devm_of_phy_get(dev, np, NULL);
449 if (IS_ERR(ep->phy)) {
450 if (PTR_ERR(ep->phy) != -ENODEV)
451 return PTR_ERR(ep->phy);
456 if (ep->ops && ep->ops->get_mem_resources) {
457 ret = ep->ops->get_mem_resources(pdev, ep);
462 if (ep->ops && ep->ops->get_clk_resources &&
463 ep->ops->init_clk_resources) {
464 ret = ep->ops->get_clk_resources(ep);
467 ret = ep->ops->init_clk_resources(ep);
472 platform_set_drvdata(pdev, ep);
474 ret = exynos_add_pcie_port(ep, pdev);
483 if (ep->ops && ep->ops->deinit_clk_resources)
484 ep->ops->deinit_clk_resources(ep);
488 static int __exit exynos_pcie_remove(struct platform_device *pdev)
490 struct exynos_pcie *ep = platform_get_drvdata(pdev);
492 if (ep->ops && ep->ops->deinit_clk_resources)
493 ep->ops->deinit_clk_resources(ep);
498 static const struct of_device_id exynos_pcie_of_match[] = {
500 .compatible = "samsung,exynos5440-pcie",
501 .data = &exynos5440_pcie_ops
506 static struct platform_driver exynos_pcie_driver = {
507 .remove = __exit_p(exynos_pcie_remove),
509 .name = "exynos-pcie",
510 .of_match_table = exynos_pcie_of_match,
514 /* Exynos PCIe driver does not allow module unload */
516 static int __init exynos_pcie_init(void)
518 return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
520 subsys_initcall(exynos_pcie_init);