Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 2 | /* |
| 3 | * PCIe RC driver for Synopsys DesignWare Core |
| 4 | * |
| 5 | * Copyright (C) 2015-2016 Synopsys, Inc. (www.synopsys.com) |
| 6 | * |
Joao Pinto | 9f46107 | 2016-11-15 16:10:47 +0000 | [diff] [blame] | 7 | * Authors: Joao Pinto <Joao.Pinto@synopsys.com> |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 8 | */ |
| 9 | #include <linux/clk.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/gpio.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/kernel.h> |
Paul Gortmaker | ca8d334 | 2016-07-02 19:13:23 -0400 | [diff] [blame] | 14 | #include <linux/init.h> |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 15 | #include <linux/of_device.h> |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 16 | #include <linux/of_gpio.h> |
| 17 | #include <linux/pci.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/resource.h> |
| 20 | #include <linux/signal.h> |
| 21 | #include <linux/types.h> |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 22 | #include <linux/regmap.h> |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 23 | |
| 24 | #include "pcie-designware.h" |
| 25 | |
| 26 | struct dw_plat_pcie { |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 27 | struct dw_pcie *pci; |
| 28 | struct regmap *regmap; |
| 29 | enum dw_pcie_device_mode mode; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 30 | }; |
| 31 | |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 32 | struct dw_plat_pcie_of_data { |
| 33 | enum dw_pcie_device_mode mode; |
| 34 | }; |
| 35 | |
| 36 | static const struct of_device_id dw_plat_pcie_of_match[]; |
| 37 | |
Bjorn Andersson | 4a30176 | 2017-07-15 23:39:45 -0700 | [diff] [blame] | 38 | static int dw_plat_pcie_host_init(struct pcie_port *pp) |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 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); |
Bjorn Andersson | 4a30176 | 2017-07-15 23:39:45 -0700 | [diff] [blame] | 47 | |
| 48 | return 0; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 49 | } |
| 50 | |
Gustavo Pimentel | 2d27ae8 | 2018-05-14 16:06:33 +0100 | [diff] [blame] | 51 | static void dw_plat_set_num_vectors(struct pcie_port *pp) |
| 52 | { |
| 53 | pp->num_vectors = MAX_MSI_IRQS; |
| 54 | } |
| 55 | |
Jisheng Zhang | 4ab2e7c | 2017-06-05 16:53:46 +0800 | [diff] [blame] | 56 | static const struct dw_pcie_host_ops dw_plat_pcie_host_ops = { |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 57 | .host_init = dw_plat_pcie_host_init, |
Gustavo Pimentel | 2d27ae8 | 2018-05-14 16:06:33 +0100 | [diff] [blame] | 58 | .set_num_vectors = dw_plat_set_num_vectors, |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 59 | }; |
| 60 | |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 61 | static int dw_plat_pcie_establish_link(struct dw_pcie *pci) |
| 62 | { |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static const struct dw_pcie_ops dw_pcie_ops = { |
| 67 | .start_link = dw_plat_pcie_establish_link, |
| 68 | }; |
| 69 | |
| 70 | static void dw_plat_pcie_ep_init(struct dw_pcie_ep *ep) |
| 71 | { |
| 72 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 73 | enum pci_barno bar; |
| 74 | |
| 75 | for (bar = BAR_0; bar <= BAR_5; bar++) |
| 76 | dw_pcie_ep_reset_bar(pci, bar); |
| 77 | } |
| 78 | |
| 79 | static int dw_plat_pcie_ep_raise_irq(struct dw_pcie_ep *ep, u8 func_no, |
| 80 | enum pci_epc_irq_type type, |
| 81 | u8 interrupt_num) |
| 82 | { |
| 83 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 84 | |
| 85 | switch (type) { |
| 86 | case PCI_EPC_IRQ_LEGACY: |
| 87 | dev_err(pci->dev, "EP cannot trigger legacy IRQs\n"); |
| 88 | return -EINVAL; |
| 89 | case PCI_EPC_IRQ_MSI: |
| 90 | return dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num); |
| 91 | default: |
| 92 | dev_err(pci->dev, "UNKNOWN IRQ type\n"); |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static struct dw_pcie_ep_ops pcie_ep_ops = { |
| 99 | .ep_init = dw_plat_pcie_ep_init, |
| 100 | .raise_irq = dw_plat_pcie_ep_raise_irq, |
| 101 | }; |
| 102 | |
| 103 | static int dw_plat_add_pcie_port(struct dw_plat_pcie *dw_plat_pcie, |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 104 | struct platform_device *pdev) |
| 105 | { |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 106 | struct dw_pcie *pci = dw_plat_pcie->pci; |
| 107 | struct pcie_port *pp = &pci->pp; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 108 | struct device *dev = &pdev->dev; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 109 | int ret; |
| 110 | |
| 111 | pp->irq = platform_get_irq(pdev, 1); |
| 112 | if (pp->irq < 0) |
| 113 | return pp->irq; |
| 114 | |
| 115 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 116 | pp->msi_irq = platform_get_irq(pdev, 0); |
| 117 | if (pp->msi_irq < 0) |
| 118 | return pp->msi_irq; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | pp->root_bus_nr = -1; |
| 122 | pp->ops = &dw_plat_pcie_host_ops; |
| 123 | |
| 124 | ret = dw_pcie_host_init(pp); |
| 125 | if (ret) { |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 126 | dev_err(dev, "Failed to initialize host\n"); |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 133 | static int dw_plat_add_pcie_ep(struct dw_plat_pcie *dw_plat_pcie, |
| 134 | struct platform_device *pdev) |
| 135 | { |
| 136 | int ret; |
| 137 | struct dw_pcie_ep *ep; |
| 138 | struct resource *res; |
| 139 | struct device *dev = &pdev->dev; |
| 140 | struct dw_pcie *pci = dw_plat_pcie->pci; |
| 141 | |
| 142 | ep = &pci->ep; |
| 143 | ep->ops = &pcie_ep_ops; |
| 144 | |
| 145 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi2"); |
| 146 | pci->dbi_base2 = devm_ioremap_resource(dev, res); |
| 147 | if (IS_ERR(pci->dbi_base2)) |
| 148 | return PTR_ERR(pci->dbi_base2); |
| 149 | |
| 150 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space"); |
| 151 | if (!res) |
| 152 | return -EINVAL; |
| 153 | |
| 154 | ep->phys_base = res->start; |
| 155 | ep->addr_size = resource_size(res); |
| 156 | |
| 157 | ret = dw_pcie_ep_init(ep); |
| 158 | if (ret) { |
| 159 | dev_err(dev, "Failed to initialize endpoint\n"); |
| 160 | return ret; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
Niklas Cassel | 794a860 | 2017-04-03 17:35:12 -0500 | [diff] [blame] | 164 | |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 165 | static int dw_plat_pcie_probe(struct platform_device *pdev) |
| 166 | { |
Bjorn Helgaas | 2d6054b | 2016-10-06 13:32:15 -0500 | [diff] [blame] | 167 | struct device *dev = &pdev->dev; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 168 | struct dw_plat_pcie *dw_plat_pcie; |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 169 | struct dw_pcie *pci; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 170 | struct resource *res; /* Resource from DT */ |
| 171 | int ret; |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 172 | const struct of_device_id *match; |
| 173 | const struct dw_plat_pcie_of_data *data; |
| 174 | enum dw_pcie_device_mode mode; |
| 175 | |
| 176 | match = of_match_device(dw_plat_pcie_of_match, dev); |
| 177 | if (!match) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | data = (struct dw_plat_pcie_of_data *)match->data; |
| 181 | mode = (enum dw_pcie_device_mode)data->mode; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 182 | |
Bjorn Helgaas | 2d6054b | 2016-10-06 13:32:15 -0500 | [diff] [blame] | 183 | dw_plat_pcie = devm_kzalloc(dev, sizeof(*dw_plat_pcie), GFP_KERNEL); |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 184 | if (!dw_plat_pcie) |
| 185 | return -ENOMEM; |
| 186 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 187 | pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); |
| 188 | if (!pci) |
| 189 | return -ENOMEM; |
| 190 | |
| 191 | pci->dev = dev; |
Niklas Cassel | 794a860 | 2017-04-03 17:35:12 -0500 | [diff] [blame] | 192 | pci->ops = &dw_pcie_ops; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 193 | |
Guenter Roeck | c046406 | 2017-02-25 02:08:12 -0800 | [diff] [blame] | 194 | dw_plat_pcie->pci = pci; |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 195 | dw_plat_pcie->mode = mode; |
Guenter Roeck | c046406 | 2017-02-25 02:08:12 -0800 | [diff] [blame] | 196 | |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 197 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi"); |
| 198 | if (!res) |
| 199 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 200 | |
Kishon Vijay Abraham I | 442ec4c | 2017-02-15 18:48:14 +0530 | [diff] [blame] | 201 | pci->dbi_base = devm_ioremap_resource(dev, res); |
| 202 | if (IS_ERR(pci->dbi_base)) |
| 203 | return PTR_ERR(pci->dbi_base); |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 204 | |
Kishon Vijay Abraham I | 9bcf0a6 | 2017-02-15 18:48:11 +0530 | [diff] [blame] | 205 | platform_set_drvdata(pdev, dw_plat_pcie); |
| 206 | |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 207 | switch (dw_plat_pcie->mode) { |
| 208 | case DW_PCIE_RC_TYPE: |
| 209 | if (!IS_ENABLED(CONFIG_PCIE_DW_PLAT_HOST)) |
| 210 | return -ENODEV; |
| 211 | |
| 212 | ret = dw_plat_add_pcie_port(dw_plat_pcie, pdev); |
| 213 | if (ret < 0) |
| 214 | return ret; |
| 215 | break; |
| 216 | case DW_PCIE_EP_TYPE: |
| 217 | if (!IS_ENABLED(CONFIG_PCIE_DW_PLAT_EP)) |
| 218 | return -ENODEV; |
| 219 | |
| 220 | ret = dw_plat_add_pcie_ep(dw_plat_pcie, pdev); |
| 221 | if (ret < 0) |
| 222 | return ret; |
| 223 | break; |
| 224 | default: |
| 225 | dev_err(dev, "INVALID device type %d\n", dw_plat_pcie->mode); |
| 226 | } |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 227 | |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 231 | static const struct dw_plat_pcie_of_data dw_plat_pcie_rc_of_data = { |
| 232 | .mode = DW_PCIE_RC_TYPE, |
| 233 | }; |
| 234 | |
| 235 | static const struct dw_plat_pcie_of_data dw_plat_pcie_ep_of_data = { |
| 236 | .mode = DW_PCIE_EP_TYPE, |
| 237 | }; |
| 238 | |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 239 | static const struct of_device_id dw_plat_pcie_of_match[] = { |
Gustavo Pimentel | 1d906b2 | 2018-05-15 15:41:42 +0100 | [diff] [blame] | 240 | { |
| 241 | .compatible = "snps,dw-pcie", |
| 242 | .data = &dw_plat_pcie_rc_of_data, |
| 243 | }, |
| 244 | { |
| 245 | .compatible = "snps,dw-pcie-ep", |
| 246 | .data = &dw_plat_pcie_ep_of_data, |
| 247 | }, |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 248 | {}, |
| 249 | }; |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 250 | |
| 251 | static struct platform_driver dw_plat_pcie_driver = { |
| 252 | .driver = { |
| 253 | .name = "dw-pcie", |
| 254 | .of_match_table = dw_plat_pcie_of_match, |
Brian Norris | a5f40e8 | 2017-04-20 15:36:25 -0500 | [diff] [blame] | 255 | .suppress_bind_attrs = true, |
Joao Pinto | 5a3aa2a | 2016-03-10 14:44:52 -0600 | [diff] [blame] | 256 | }, |
| 257 | .probe = dw_plat_pcie_probe, |
| 258 | }; |
Paul Gortmaker | ca8d334 | 2016-07-02 19:13:23 -0400 | [diff] [blame] | 259 | builtin_platform_driver(dw_plat_pcie_driver); |