blob: f30f5f3fb5c1e61505fd7fa29f55e009ae9ae0bb [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Ray Jui1fb37a82015-04-08 11:21:35 -07002/*
3 * Copyright (C) 2015 Broadcom Corporation
Ray Jui1fb37a82015-04-08 11:21:35 -07004 */
5
6#include <linux/kernel.h>
7#include <linux/pci.h>
8#include <linux/clk.h>
9#include <linux/module.h>
10#include <linux/slab.h>
11#include <linux/interrupt.h>
12#include <linux/platform_device.h>
13#include <linux/of_address.h>
14#include <linux/of_pci.h>
15#include <linux/of_irq.h>
16#include <linux/of_platform.h>
17#include <linux/phy/phy.h>
18
Rob Herring9e2aee82018-05-11 12:15:30 -050019#include "../pci.h"
Ray Jui1fb37a82015-04-08 11:21:35 -070020#include "pcie-iproc.h"
21
Ray Jui943ebae2015-12-04 09:34:59 -080022static const struct of_device_id iproc_pcie_of_match_table[] = {
23 {
24 .compatible = "brcm,iproc-pcie",
25 .data = (int *)IPROC_PCIE_PAXB,
26 }, {
Ray Juic7c44522016-10-31 17:38:41 -070027 .compatible = "brcm,iproc-pcie-paxb-v2",
28 .data = (int *)IPROC_PCIE_PAXB_V2,
29 }, {
Ray Jui943ebae2015-12-04 09:34:59 -080030 .compatible = "brcm,iproc-pcie-paxc",
31 .data = (int *)IPROC_PCIE_PAXC,
Ray Jui787b3c42016-10-31 17:38:35 -070032 }, {
33 .compatible = "brcm,iproc-pcie-paxc-v2",
34 .data = (int *)IPROC_PCIE_PAXC_V2,
Ray Jui943ebae2015-12-04 09:34:59 -080035 },
36 { /* sentinel */ }
37};
38MODULE_DEVICE_TABLE(of, iproc_pcie_of_match_table);
39
Ray Jui1fb37a82015-04-08 11:21:35 -070040static int iproc_pcie_pltfm_probe(struct platform_device *pdev)
41{
Bjorn Helgaas786aecc2016-10-06 13:36:08 -050042 struct device *dev = &pdev->dev;
Ray Jui1fb37a82015-04-08 11:21:35 -070043 struct iproc_pcie *pcie;
Bjorn Helgaas786aecc2016-10-06 13:36:08 -050044 struct device_node *np = dev->of_node;
Ray Jui1fb37a82015-04-08 11:21:35 -070045 struct resource reg;
46 resource_size_t iobase = 0;
Bjorn Helgaas6e347b52017-03-09 11:27:07 -060047 LIST_HEAD(resources);
Lorenzo Pieralisi52774072017-06-28 15:13:57 -050048 struct pci_host_bridge *bridge;
Ray Jui1fb37a82015-04-08 11:21:35 -070049 int ret;
50
Lorenzo Pieralisi52774072017-06-28 15:13:57 -050051 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
52 if (!bridge)
Ray Jui1fb37a82015-04-08 11:21:35 -070053 return -ENOMEM;
54
Lorenzo Pieralisi52774072017-06-28 15:13:57 -050055 pcie = pci_host_bridge_priv(bridge);
56
Bjorn Helgaas786aecc2016-10-06 13:36:08 -050057 pcie->dev = dev;
Bjorn Helgaase5c3b3e2017-01-31 16:36:32 -060058 pcie->type = (enum iproc_pcie_type) of_device_get_match_data(dev);
Ray Jui1fb37a82015-04-08 11:21:35 -070059
60 ret = of_address_to_resource(np, 0, &reg);
61 if (ret < 0) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -050062 dev_err(dev, "unable to obtain controller resources\n");
Ray Jui1fb37a82015-04-08 11:21:35 -070063 return ret;
64 }
65
Lorenzo Pieralisi868564d2017-04-19 17:49:02 +010066 pcie->base = devm_pci_remap_cfgspace(dev, reg.start,
67 resource_size(&reg));
Ray Jui1fb37a82015-04-08 11:21:35 -070068 if (!pcie->base) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -050069 dev_err(dev, "unable to map controller registers\n");
Ray Jui1fb37a82015-04-08 11:21:35 -070070 return -ENOMEM;
71 }
Ray Jui3bc2b232016-01-06 18:04:35 -060072 pcie->base_addr = reg.start;
Ray Jui1fb37a82015-04-08 11:21:35 -070073
Ray Juie99a1872015-10-16 08:18:24 -050074 if (of_property_read_bool(np, "brcm,pcie-ob")) {
75 u32 val;
76
77 ret = of_property_read_u32(np, "brcm,pcie-ob-axi-offset",
78 &val);
79 if (ret) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -050080 dev_err(dev,
Ray Juie99a1872015-10-16 08:18:24 -050081 "missing brcm,pcie-ob-axi-offset property\n");
82 return ret;
83 }
84 pcie->ob.axi_offset = val;
Ray Juie99a1872015-10-16 08:18:24 -050085 pcie->need_ob_cfg = true;
86 }
87
Ray Jui3b65ca52018-01-11 12:36:16 -080088 /*
89 * DT nodes are not used by all platforms that use the iProc PCIe
90 * core driver. For platforms that require explict inbound mapping
91 * configuration, "dma-ranges" would have been present in DT
92 */
93 pcie->need_ib_cfg = of_property_read_bool(np, "dma-ranges");
94
Ray Jui1fb37a82015-04-08 11:21:35 -070095 /* PHY use is optional */
Bjorn Helgaas786aecc2016-10-06 13:36:08 -050096 pcie->phy = devm_phy_get(dev, "pcie-phy");
Ray Jui1fb37a82015-04-08 11:21:35 -070097 if (IS_ERR(pcie->phy)) {
98 if (PTR_ERR(pcie->phy) == -EPROBE_DEFER)
99 return -EPROBE_DEFER;
100 pcie->phy = NULL;
101 }
102
Jan Kiszka5bd51b32018-05-15 11:07:05 +0200103 ret = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, &resources,
Jan Kiszka055f87a2018-05-15 11:07:03 +0200104 &iobase);
Ray Jui1fb37a82015-04-08 11:21:35 -0700105 if (ret) {
Bjorn Helgaas6e347b52017-03-09 11:27:07 -0600106 dev_err(dev, "unable to get PCI host bridge resources\n");
Ray Jui1fb37a82015-04-08 11:21:35 -0700107 return ret;
108 }
109
Andy Gospodarekffbd7962016-12-01 15:34:52 -0500110 /* PAXC doesn't support legacy IRQs, skip mapping */
111 switch (pcie->type) {
112 case IPROC_PCIE_PAXC:
113 case IPROC_PCIE_PAXC_V2:
114 break;
115 default:
116 pcie->map_irq = of_irq_parse_and_map_pci;
117 }
Hauke Mehrtensc1e02ce2015-05-12 23:23:00 +0200118
Bjorn Helgaas6e347b52017-03-09 11:27:07 -0600119 ret = iproc_pcie_setup(pcie, &resources);
120 if (ret) {
Bjorn Helgaas786aecc2016-10-06 13:36:08 -0500121 dev_err(dev, "PCIe controller setup failed\n");
Bjorn Helgaas6e347b52017-03-09 11:27:07 -0600122 pci_free_resource_list(&resources);
123 return ret;
124 }
Hauke Mehrtensef079912015-05-24 22:37:03 +0200125
Bjorn Helgaas556c7bb2016-10-06 13:36:08 -0500126 platform_set_drvdata(pdev, pcie);
Bjorn Helgaas6e347b52017-03-09 11:27:07 -0600127 return 0;
Ray Jui1fb37a82015-04-08 11:21:35 -0700128}
129
130static int iproc_pcie_pltfm_remove(struct platform_device *pdev)
131{
132 struct iproc_pcie *pcie = platform_get_drvdata(pdev);
133
134 return iproc_pcie_remove(pcie);
135}
136
Oza Pawandeepb91c26c2017-08-28 16:43:35 -0500137static void iproc_pcie_pltfm_shutdown(struct platform_device *pdev)
138{
139 struct iproc_pcie *pcie = platform_get_drvdata(pdev);
140
141 iproc_pcie_shutdown(pcie);
142}
143
Ray Jui1fb37a82015-04-08 11:21:35 -0700144static struct platform_driver iproc_pcie_pltfm_driver = {
145 .driver = {
146 .name = "iproc-pcie",
147 .of_match_table = of_match_ptr(iproc_pcie_of_match_table),
148 },
149 .probe = iproc_pcie_pltfm_probe,
150 .remove = iproc_pcie_pltfm_remove,
Oza Pawandeepb91c26c2017-08-28 16:43:35 -0500151 .shutdown = iproc_pcie_pltfm_shutdown,
Ray Jui1fb37a82015-04-08 11:21:35 -0700152};
153module_platform_driver(iproc_pcie_pltfm_driver);
154
155MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
156MODULE_DESCRIPTION("Broadcom iPROC PCIe platform driver");
157MODULE_LICENSE("GPL v2");