| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Helper routines to scan the device tree for PCI devices and busses |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 8 | 9 | * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM |
|---|
| 9 | 10 | * Rework, based on alpha PCI code. |
|---|
| 10 | 11 | * Copyright (c) 2009 Secret Lab Technologies Ltd. |
|---|
| 11 | | - * |
|---|
| 12 | | - * This program is free software; you can redistribute it and/or |
|---|
| 13 | | - * modify it under the terms of the GNU General Public License |
|---|
| 14 | | - * version 2 as published by the Free Software Foundation. |
|---|
| 15 | 12 | */ |
|---|
| 16 | 13 | |
|---|
| 17 | 14 | #include <linux/pci.h> |
|---|
| .. | .. |
|---|
| 37 | 34 | * pci_parse_of_flags - Parse the flags cell of a device tree PCI address |
|---|
| 38 | 35 | * @addr0: value of 1st cell of a device tree PCI address. |
|---|
| 39 | 36 | * @bridge: Set this flag if the address is from a bridge 'ranges' property |
|---|
| 37 | + * |
|---|
| 38 | + * PCI Bus Binding to IEEE Std 1275-1994 |
|---|
| 39 | + * |
|---|
| 40 | + * Bit# 33222222 22221111 11111100 00000000 |
|---|
| 41 | + * 10987654 32109876 54321098 76543210 |
|---|
| 42 | + * phys.hi cell: npt000ss bbbbbbbb dddddfff rrrrrrrr |
|---|
| 43 | + * phys.mid cell: hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh |
|---|
| 44 | + * phys.lo cell: llllllll llllllll llllllll llllllll |
|---|
| 45 | + * |
|---|
| 46 | + * where: |
|---|
| 47 | + * n is 0 if the address is relocatable, 1 otherwise |
|---|
| 48 | + * p is 1 if the addressable region is "prefetchable", 0 otherwise |
|---|
| 49 | + * t is 1 if the address is aliased (for non-relocatable I/O), |
|---|
| 50 | + * below 1 MB (for Memory),or below 64 KB (for relocatable I/O). |
|---|
| 51 | + * ss is the space code, denoting the address space: |
|---|
| 52 | + * 00 denotes Configuration Space |
|---|
| 53 | + * 01 denotes I/O Space |
|---|
| 54 | + * 10 denotes 32-bit-address Memory Space |
|---|
| 55 | + * 11 denotes 64-bit-address Memory Space |
|---|
| 56 | + * bbbbbbbb is the 8-bit Bus Number |
|---|
| 57 | + * ddddd is the 5-bit Device Number |
|---|
| 58 | + * fff is the 3-bit Function Number |
|---|
| 59 | + * rrrrrrrr is the 8-bit Register Number |
|---|
| 40 | 60 | */ |
|---|
| 61 | +#define OF_PCI_ADDR0_SPACE(ss) (((ss)&3)<<24) |
|---|
| 62 | +#define OF_PCI_ADDR0_SPACE_CFG OF_PCI_ADDR0_SPACE(0) |
|---|
| 63 | +#define OF_PCI_ADDR0_SPACE_IO OF_PCI_ADDR0_SPACE(1) |
|---|
| 64 | +#define OF_PCI_ADDR0_SPACE_MMIO32 OF_PCI_ADDR0_SPACE(2) |
|---|
| 65 | +#define OF_PCI_ADDR0_SPACE_MMIO64 OF_PCI_ADDR0_SPACE(3) |
|---|
| 66 | +#define OF_PCI_ADDR0_SPACE_MASK OF_PCI_ADDR0_SPACE(3) |
|---|
| 67 | +#define OF_PCI_ADDR0_RELOC (1UL<<31) |
|---|
| 68 | +#define OF_PCI_ADDR0_PREFETCH (1UL<<30) |
|---|
| 69 | +#define OF_PCI_ADDR0_ALIAS (1UL<<29) |
|---|
| 70 | +#define OF_PCI_ADDR0_BUS 0x00FF0000UL |
|---|
| 71 | +#define OF_PCI_ADDR0_DEV 0x0000F800UL |
|---|
| 72 | +#define OF_PCI_ADDR0_FN 0x00000700UL |
|---|
| 73 | +#define OF_PCI_ADDR0_BARREG 0x000000FFUL |
|---|
| 74 | + |
|---|
| 41 | 75 | unsigned int pci_parse_of_flags(u32 addr0, int bridge) |
|---|
| 42 | 76 | { |
|---|
| 43 | | - unsigned int flags = 0; |
|---|
| 77 | + unsigned int flags = 0, as = addr0 & OF_PCI_ADDR0_SPACE_MASK; |
|---|
| 44 | 78 | |
|---|
| 45 | | - if (addr0 & 0x02000000) { |
|---|
| 79 | + if (as == OF_PCI_ADDR0_SPACE_MMIO32 || as == OF_PCI_ADDR0_SPACE_MMIO64) { |
|---|
| 46 | 80 | flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY; |
|---|
| 47 | | - flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64; |
|---|
| 48 | | - if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) |
|---|
| 49 | | - flags |= IORESOURCE_MEM_64; |
|---|
| 50 | | - flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M; |
|---|
| 51 | | - if (addr0 & 0x40000000) |
|---|
| 52 | | - flags |= IORESOURCE_PREFETCH |
|---|
| 53 | | - | PCI_BASE_ADDRESS_MEM_PREFETCH; |
|---|
| 81 | + |
|---|
| 82 | + if (as == OF_PCI_ADDR0_SPACE_MMIO64) |
|---|
| 83 | + flags |= PCI_BASE_ADDRESS_MEM_TYPE_64 | IORESOURCE_MEM_64; |
|---|
| 84 | + |
|---|
| 85 | + if (addr0 & OF_PCI_ADDR0_ALIAS) |
|---|
| 86 | + flags |= PCI_BASE_ADDRESS_MEM_TYPE_1M; |
|---|
| 87 | + |
|---|
| 88 | + if (addr0 & OF_PCI_ADDR0_PREFETCH) |
|---|
| 89 | + flags |= IORESOURCE_PREFETCH | |
|---|
| 90 | + PCI_BASE_ADDRESS_MEM_PREFETCH; |
|---|
| 91 | + |
|---|
| 54 | 92 | /* Note: We don't know whether the ROM has been left enabled |
|---|
| 55 | 93 | * by the firmware or not. We mark it as disabled (ie, we do |
|---|
| 56 | 94 | * not set the IORESOURCE_ROM_ENABLE flag) for now rather than |
|---|
| 57 | 95 | * do a config space read, it will be force-enabled if needed |
|---|
| 58 | 96 | */ |
|---|
| 59 | | - if (!bridge && (addr0 & 0xff) == 0x30) |
|---|
| 97 | + if (!bridge && (addr0 & OF_PCI_ADDR0_BARREG) == PCI_ROM_ADDRESS) |
|---|
| 60 | 98 | flags |= IORESOURCE_READONLY; |
|---|
| 61 | | - } else if (addr0 & 0x01000000) |
|---|
| 99 | + |
|---|
| 100 | + } else if (as == OF_PCI_ADDR0_SPACE_IO) |
|---|
| 62 | 101 | flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO; |
|---|
| 102 | + |
|---|
| 63 | 103 | if (flags) |
|---|
| 64 | 104 | flags |= IORESOURCE_SIZEALIGN; |
|---|
| 105 | + |
|---|
| 65 | 106 | return flags; |
|---|
| 66 | 107 | } |
|---|
| 67 | 108 | |
|---|
| .. | .. |
|---|
| 135 | 176 | struct pci_bus *bus, int devfn) |
|---|
| 136 | 177 | { |
|---|
| 137 | 178 | struct pci_dev *dev; |
|---|
| 138 | | - const char *type; |
|---|
| 139 | 179 | |
|---|
| 140 | 180 | dev = pci_alloc_dev(bus); |
|---|
| 141 | 181 | if (!dev) |
|---|
| 142 | 182 | return NULL; |
|---|
| 143 | | - type = of_get_property(node, "device_type", NULL); |
|---|
| 144 | | - if (type == NULL) |
|---|
| 145 | | - type = ""; |
|---|
| 146 | 183 | |
|---|
| 147 | | - pr_debug(" create device, devfn: %x, type: %s\n", devfn, type); |
|---|
| 184 | + pr_debug(" create device, devfn: %x, type: %s\n", devfn, |
|---|
| 185 | + of_node_get_device_type(node)); |
|---|
| 148 | 186 | |
|---|
| 149 | 187 | dev->dev.of_node = of_node_get(node); |
|---|
| 150 | 188 | dev->dev.parent = bus->bridge; |
|---|
| .. | .. |
|---|
| 177 | 215 | /* Early fixups, before probing the BARs */ |
|---|
| 178 | 216 | pci_fixup_device(pci_fixup_early, dev); |
|---|
| 179 | 217 | |
|---|
| 180 | | - if (!strcmp(type, "pci") || !strcmp(type, "pciex")) { |
|---|
| 218 | + if (of_node_is_type(node, "pci") || of_node_is_type(node, "pciex")) { |
|---|
| 181 | 219 | /* a PCI-PCI bridge */ |
|---|
| 182 | 220 | dev->hdr_type = PCI_HEADER_TYPE_BRIDGE; |
|---|
| 183 | 221 | dev->rom_base_reg = PCI_ROM_ADDRESS1; |
|---|
| 184 | 222 | set_pcie_hotplug_bridge(dev); |
|---|
| 185 | | - } else if (!strcmp(type, "cardbus")) { |
|---|
| 223 | + } else if (of_node_is_type(node, "cardbus")) { |
|---|
| 186 | 224 | dev->hdr_type = PCI_HEADER_TYPE_CARDBUS; |
|---|
| 187 | 225 | } else { |
|---|
| 188 | 226 | dev->hdr_type = PCI_HEADER_TYPE_NORMAL; |
|---|
| .. | .. |
|---|
| 376 | 414 | */ |
|---|
| 377 | 415 | if (!rescan_existing) |
|---|
| 378 | 416 | pcibios_setup_bus_self(bus); |
|---|
| 379 | | - pcibios_setup_bus_devices(bus); |
|---|
| 380 | 417 | |
|---|
| 381 | 418 | /* Now scan child busses */ |
|---|
| 382 | 419 | for_each_pci_bridge(dev, bus) |
|---|