1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2015 Broadcom Corporation
4  */
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 
19 #include "../pci.h"
20 #include "pcie-iproc.h"
21 
22 static const struct of_device_id iproc_pcie_of_match_table[] = {
23 	{
24 		.compatible = "brcm,iproc-pcie",
25 		.data = (int *)IPROC_PCIE_PAXB,
26 	}, {
27 		.compatible = "brcm,iproc-pcie-paxb-v2",
28 		.data = (int *)IPROC_PCIE_PAXB_V2,
29 	}, {
30 		.compatible = "brcm,iproc-pcie-paxc",
31 		.data = (int *)IPROC_PCIE_PAXC,
32 	}, {
33 		.compatible = "brcm,iproc-pcie-paxc-v2",
34 		.data = (int *)IPROC_PCIE_PAXC_V2,
35 	},
36 	{ /* sentinel */ }
37 };
38 MODULE_DEVICE_TABLE(of, iproc_pcie_of_match_table);
39 
40 static int iproc_pcie_pltfm_probe(struct platform_device *pdev)
41 {
42 	struct device *dev = &pdev->dev;
43 	struct iproc_pcie *pcie;
44 	struct device_node *np = dev->of_node;
45 	struct resource reg;
46 	resource_size_t iobase = 0;
47 	LIST_HEAD(resources);
48 	struct pci_host_bridge *bridge;
49 	int ret;
50 
51 	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
52 	if (!bridge)
53 		return -ENOMEM;
54 
55 	pcie = pci_host_bridge_priv(bridge);
56 
57 	pcie->dev = dev;
58 	pcie->type = (enum iproc_pcie_type) of_device_get_match_data(dev);
59 
60 	ret = of_address_to_resource(np, 0, &reg);
61 	if (ret < 0) {
62 		dev_err(dev, "unable to obtain controller resources\n");
63 		return ret;
64 	}
65 
66 	pcie->base = devm_pci_remap_cfgspace(dev, reg.start,
67 					     resource_size(&reg));
68 	if (!pcie->base) {
69 		dev_err(dev, "unable to map controller registers\n");
70 		return -ENOMEM;
71 	}
72 	pcie->base_addr = reg.start;
73 
74 	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) {
80 			dev_err(dev,
81 				"missing brcm,pcie-ob-axi-offset property\n");
82 			return ret;
83 		}
84 		pcie->ob.axi_offset = val;
85 		pcie->need_ob_cfg = true;
86 	}
87 
88 	/*
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 
95 	/* PHY use is optional */
96 	pcie->phy = devm_phy_get(dev, "pcie-phy");
97 	if (IS_ERR(pcie->phy)) {
98 		if (PTR_ERR(pcie->phy) == -EPROBE_DEFER)
99 			return -EPROBE_DEFER;
100 		pcie->phy = NULL;
101 	}
102 
103 	ret = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, &resources,
104 						    &iobase);
105 	if (ret) {
106 		dev_err(dev, "unable to get PCI host bridge resources\n");
107 		return ret;
108 	}
109 
110 	/* 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 	}
118 
119 	ret = iproc_pcie_setup(pcie, &resources);
120 	if (ret) {
121 		dev_err(dev, "PCIe controller setup failed\n");
122 		pci_free_resource_list(&resources);
123 		return ret;
124 	}
125 
126 	platform_set_drvdata(pdev, pcie);
127 	return 0;
128 }
129 
130 static 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 
137 static 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 
144 static 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,
151 	.shutdown = iproc_pcie_pltfm_shutdown,
152 };
153 module_platform_driver(iproc_pcie_pltfm_driver);
154 
155 MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
156 MODULE_DESCRIPTION("Broadcom iPROC PCIe platform driver");
157 MODULE_LICENSE("GPL v2");
158