blob: 427fc22f72b6bc77950028178863c930f3772285 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +11002/*
3 * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp.
4 * <benh@kernel.crashing.org>
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +11005 * and Arnd Bergmann, IBM Corp.
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +11006 */
7
8#undef DEBUG
9
10#include <linux/string.h>
11#include <linux/kernel.h>
12#include <linux/init.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -040013#include <linux/export.h>
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +110014#include <linux/mod_devicetable.h>
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110015#include <linux/pci.h>
Grant Likely283029d2008-01-09 06:20:40 +110016#include <linux/of.h>
Stephen Rothwellcd6eed32007-09-21 18:08:17 +100017#include <linux/of_device.h>
18#include <linux/of_platform.h>
Gavin Shaneb740b52012-02-27 20:04:04 +000019#include <linux/atomic.h>
Benjamin Herrenschmidt7eebde72006-11-11 17:24:59 +110020
21#include <asm/errno.h>
Benjamin Herrenschmidt12d04ee2006-11-11 17:25:02 +110022#include <asm/topology.h>
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110023#include <asm/pci-bridge.h>
24#include <asm/ppc-pci.h>
Gavin Shaneb740b52012-02-27 20:04:04 +000025#include <asm/eeh.h>
Benjamin Herrenschmidt93091802006-11-21 14:56:37 +110026
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110027#ifdef CONFIG_PPC_OF_PLATFORM_PCI
28
29/* The probing of PCI controllers from of_platform is currently
30 * 64 bits only, mostly due to gratuitous differences between
31 * the 32 and 64 bits PCI code on PowerPC and the 32 bits one
32 * lacking some bits needed here.
33 */
34
Greg Kroah-Hartmancad5cef2012-12-21 14:04:10 -080035static int of_pci_phb_probe(struct platform_device *dev)
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110036{
37 struct pci_controller *phb;
38
39 /* Check if we can do that ... */
40 if (ppc_md.pci_setup_phb == NULL)
41 return -ENODEV;
42
Rob Herringb7c670d2017-08-21 10:16:47 -050043 pr_info("Setting up PCI bus %pOF\n", dev->dev.of_node);
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110044
45 /* Alloc and setup PHB data structure */
Grant Likely61c7a082010-04-13 16:12:29 -070046 phb = pcibios_alloc_controller(dev->dev.of_node);
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110047 if (!phb)
48 return -ENODEV;
49
50 /* Setup parent in sysfs */
51 phb->parent = &dev->dev;
52
53 /* Setup the PHB using arch provided callback */
54 if (ppc_md.pci_setup_phb(phb)) {
55 pcibios_free_controller(phb);
56 return -ENODEV;
57 }
58
59 /* Process "ranges" property */
Grant Likely61c7a082010-04-13 16:12:29 -070060 pci_process_bridge_OF_ranges(phb, dev->dev.of_node, 0);
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110061
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110062 /* Init pci_dn data structures */
63 pci_devs_phb_init_dynamic(phb);
64
Gavin Shaneb740b52012-02-27 20:04:04 +000065 /* Create EEH devices for the PHB */
66 eeh_dev_phb_init_dynamic(phb);
67
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110068 /* Register devices with EEH */
Grant Likely61c7a082010-04-13 16:12:29 -070069 if (dev->dev.of_node->child)
Gavin Shanff57b452015-03-17 16:15:06 +110070 eeh_add_device_tree_early(PCI_DN(dev->dev.of_node));
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110071
72 /* Scan the bus */
Grant Likelyb5d937d2011-02-04 11:24:11 -070073 pcibios_scan_phb(phb);
Ishizaki Kou7cfb62a2008-04-24 19:21:10 +100074 if (phb->bus == NULL)
75 return -ENXIO;
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110076
77 /* Claim resources. This might need some rework as well depending
Adam Buchbinder48fc7f72012-09-19 21:48:00 -040078 * whether we are doing probe-only or not, like assigning unassigned
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110079 * resources etc...
80 */
81 pcibios_claim_one_bus(phb->bus);
82
83 /* Finish EEH setup */
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110084 eeh_add_device_tree_late(phb->bus);
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110085
86 /* Add probed PCI devices to the device model */
87 pci_bus_add_devices(phb->bus);
88
Thadeu Lima de Souza Cascardo6a040ce2012-12-28 09:13:19 +000089 /* sysfs files should only be added after devices are added */
90 eeh_add_sysfs_files(phb->bus);
91
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110092 return 0;
93}
94
Uwe Kleine-Königce6d73c2014-09-10 21:56:38 +020095static const struct of_device_id of_pci_phb_ids[] = {
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +110096 { .type = "pci", },
97 { .type = "pcix", },
98 { .type = "pcie", },
99 { .type = "pciex", },
100 { .type = "ht", },
101 {}
102};
103
Grant Likely00006122011-02-22 19:59:54 -0700104static struct platform_driver of_pci_phb_driver = {
Stephen Rothwellcd6eed32007-09-21 18:08:17 +1000105 .probe = of_pci_phb_probe,
106 .driver = {
107 .name = "of-pci",
Grant Likely40182942010-04-13 16:13:02 -0700108 .of_match_table = of_pci_phb_ids,
Stephen Rothwellcd6eed32007-09-21 18:08:17 +1000109 },
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +1100110};
111
Geliang Tangebb242d2016-11-23 22:58:56 +0800112builtin_platform_driver(of_pci_phb_driver);
Benjamin Herrenschmidt4c9d2802006-11-11 17:25:08 +1100113
114#endif /* CONFIG_PPC_OF_PLATFORM_PCI */