Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 1 | /* |
| 2 | * PCIe RC driver for Synopsys DesignWare Core |
| 3 | * |
| 4 | * Copyright (C) 2015-2016 Synopsys, Inc. (www.synopsys.com) |
| 5 | * |
Joao Pinto | 9f46107 | 2016-11-15 16:10:47 +0000 | [diff] [blame] | 6 | * Authors: Joao Pinto <Joao.Pinto@synopsys.com> |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | #include <linux/clk.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/gpio.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/kernel.h> |
Paul Gortmaker | ca8d334 | 2016-07-02 19:13:23 -0400 | [diff] [blame] | 17 | #include <linux/init.h> |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 18 | #include <linux/of_gpio.h> |
| 19 | #include <linux/pci.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/resource.h> |
| 22 | #include <linux/signal.h> |
| 23 | #include <linux/types.h> |
| 24 | |
| 25 | #include "pcie-designware.h" |
| 26 | |
| 27 | struct dw_plat_pcie { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 28 | struct dw_pcie *pci; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static irqreturn_t dw_plat_pcie_msi_irq_handler(int irq, void *arg) |
| 32 | { |
| 33 | struct pcie_port *pp = arg; |
| 34 | |
| 35 | return dw_handle_msi_irq(pp); |
| 36 | } |
| 37 | |
| 38 | static void dw_plat_pcie_host_init(struct pcie_port *pp) |
| 39 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 40 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 41 | |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 42 | dw_pcie_setup_rc(pp); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 43 | dw_pcie_wait_for_link(pci); |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 44 | |
| 45 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
| 46 | dw_pcie_msi_init(pp); |
| 47 | } |
| 48 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 49 | static struct dw_pcie_host_ops dw_plat_pcie_host_ops = { |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 50 | .host_init = dw_plat_pcie_host_init, |
| 51 | }; |
| 52 | |
| 53 | static int dw_plat_add_pcie_port(struct pcie_port *pp, |
| 54 | struct platform_device *pdev) |
| 55 | { |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 56 | struct device *dev = &pdev->dev; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 57 | int ret; |
| 58 | |
| 59 | pp->irq = platform_get_irq(pdev, 1); |
| 60 | if (pp->irq < 0) |
| 61 | return pp->irq; |
| 62 | |
| 63 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 64 | pp->msi_irq = platform_get_irq(pdev, 0); |
| 65 | if (pp->msi_irq < 0) |
| 66 | return pp->msi_irq; |
| 67 | |
Bjorn Helgaas | 2d6054b | 2016-10-06 13:32:15 -0500 | [diff] [blame] | 68 | ret = devm_request_irq(dev, pp->msi_irq, |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 69 | dw_plat_pcie_msi_irq_handler, |
| 70 | IRQF_SHARED, "dw-plat-pcie-msi", pp); |
| 71 | if (ret) { |
Bjorn Helgaas | 2d6054b | 2016-10-06 13:32:15 -0500 | [diff] [blame] | 72 | dev_err(dev, "failed to request MSI IRQ\n"); |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 73 | return ret; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | pp->root_bus_nr = -1; |
| 78 | pp->ops = &dw_plat_pcie_host_ops; |
| 79 | |
| 80 | ret = dw_pcie_host_init(pp); |
| 81 | if (ret) { |
Bjorn Helgaas | 2d6054b | 2016-10-06 13:32:15 -0500 | [diff] [blame] | 82 | dev_err(dev, "failed to initialize host\n"); |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int dw_plat_pcie_probe(struct platform_device *pdev) |
| 90 | { |
Bjorn Helgaas | 2d6054b | 2016-10-06 13:32:15 -0500 | [diff] [blame] | 91 | struct device *dev = &pdev->dev; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 92 | struct dw_plat_pcie *dw_plat_pcie; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 93 | struct dw_pcie *pci; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 94 | struct resource *res; /* Resource from DT */ |
| 95 | int ret; |
| 96 | |
Bjorn Helgaas | 2d6054b | 2016-10-06 13:32:15 -0500 | [diff] [blame] | 97 | dw_plat_pcie = devm_kzalloc(dev, sizeof(*dw_plat_pcie), GFP_KERNEL); |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 98 | if (!dw_plat_pcie) |
| 99 | return -ENOMEM; |
| 100 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 101 | pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); |
| 102 | if (!pci) |
| 103 | return -ENOMEM; |
| 104 | |
| 105 | pci->dev = dev; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 106 | |
Guenter Roeck | c046406 | 2017-02-25 02:08:12 -0800 | [diff] [blame] | 107 | dw_plat_pcie->pci = pci; |
| 108 | |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 109 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 110 | pci->dbi_base = devm_ioremap_resource(dev, res); |
| 111 | if (IS_ERR(pci->dbi_base)) |
| 112 | return PTR_ERR(pci->dbi_base); |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 113 | |
Kishon Vijay Abraham I | 9bcf0a6 | 2017-02-15 18:48:11 +0530 | [diff] [blame] | 114 | platform_set_drvdata(pdev, dw_plat_pcie); |
| 115 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 116 | ret = dw_plat_add_pcie_port(&pci->pp, pdev); |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 117 | if (ret < 0) |
| 118 | return ret; |
| 119 | |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static const struct of_device_id dw_plat_pcie_of_match[] = { |
| 124 | { .compatible = "snps,dw-pcie", }, |
| 125 | {}, |
| 126 | }; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 127 | |
| 128 | static struct platform_driver dw_plat_pcie_driver = { |
| 129 | .driver = { |
| 130 | .name = "dw-pcie", |
| 131 | .of_match_table = dw_plat_pcie_of_match, |
| 132 | }, |
| 133 | .probe = dw_plat_pcie_probe, |
| 134 | }; |
Paul Gortmaker | ca8d334 | 2016-07-02 19:13:23 -0400 | [diff] [blame] | 135 | builtin_platform_driver(dw_plat_pcie_driver); |