1ff3e077bSSimon GlassPCI with Driver Model
2ff3e077bSSimon Glass=====================
3ff3e077bSSimon Glass
4ff3e077bSSimon GlassHow busses are scanned
5ff3e077bSSimon Glass----------------------
6ff3e077bSSimon Glass
7ff3e077bSSimon GlassAny config read will end up at pci_read_config(). This uses
8ff3e077bSSimon Glassuclass_get_device_by_seq() to get the PCI bus for a particular bus number.
9ff3e077bSSimon GlassBus number 0 will need to be requested first, and the alias in the device
10ff3e077bSSimon Glasstree file will point to the correct device:
11ff3e077bSSimon Glass
12ff3e077bSSimon Glass
13ff3e077bSSimon Glass	aliases {
14ff3e077bSSimon Glass		pci0 = &pci;
15ff3e077bSSimon Glass	};
16ff3e077bSSimon Glass
17ff3e077bSSimon Glass	pci: pci-controller {
18ff3e077bSSimon Glass		compatible = "sandbox,pci";
19ff3e077bSSimon Glass		...
20ff3e077bSSimon Glass	};
21ff3e077bSSimon Glass
22ff3e077bSSimon Glass
23ff3e077bSSimon GlassIf there is no alias the devices will be numbered sequentially in the device
24ff3e077bSSimon Glasstree.
25ff3e077bSSimon Glass
26947eb439SBin MengThe call to uclass_get_device() will cause the PCI bus to be probed.
27ff3e077bSSimon GlassThis does a scan of the bus to locate available devices. These devices are
28ff3e077bSSimon Glassbound to their appropriate driver if available. If there is no driver, then
29ff3e077bSSimon Glassthey are bound to a generic PCI driver which does nothing.
30ff3e077bSSimon Glass
31ff3e077bSSimon GlassAfter probing a bus, the available devices will appear in the device tree
32ff3e077bSSimon Glassunder that bus.
33ff3e077bSSimon Glass
34ff3e077bSSimon GlassNote that this is all done on a lazy basis, as needed, so until something is
35947eb439SBin Mengtouched on PCI (eg: a call to pci_find_devices()) it will not be probed.
36ff3e077bSSimon Glass
37*92ed9865SMarek VasutPCI devices can appear in the flattened device tree. If they do, their node
38*92ed9865SMarek Vasutoften contains extra information which cannot be derived from the PCI IDs or
39*92ed9865SMarek VasutPCI class of the device. Each PCI device node must have a <reg> property, as
40*92ed9865SMarek Vasutdefined by the IEEE Std 1275-1994 PCI bus binding document v2.1. Compatible
41*92ed9865SMarek Vasutstring list is optional and generally not needed, since PCI is discoverable
42*92ed9865SMarek Vasutbus, albeit there are justified exceptions. If the compatible string is
43*92ed9865SMarek Vasutpresent, matching on it takes precedence over PCI IDs and PCI classes.
44*92ed9865SMarek Vasut
45*92ed9865SMarek VasutNote we must describe PCI devices with the same bus hierarchy as the
46f4b5db7cSBin Menghardware, otherwise driver model cannot detect the correct parent/children
47f4b5db7cSBin Mengrelationship during PCI bus enumeration thus PCI devices won't be bound to
48f4b5db7cSBin Mengtheir drivers accordingly. A working example like below:
49f4b5db7cSBin Meng
50f4b5db7cSBin Meng	pci {
51f4b5db7cSBin Meng		#address-cells = <3>;
52f4b5db7cSBin Meng		#size-cells = <2>;
53f4b5db7cSBin Meng		compatible = "pci-x86";
54f4b5db7cSBin Meng		u-boot,dm-pre-reloc;
55f4b5db7cSBin Meng		ranges = <0x02000000 0x0 0x40000000 0x40000000 0 0x80000000
56f4b5db7cSBin Meng			  0x42000000 0x0 0xc0000000 0xc0000000 0 0x20000000
57f4b5db7cSBin Meng			  0x01000000 0x0 0x2000 0x2000 0 0xe000>;
58f4b5db7cSBin Meng
59f4b5db7cSBin Meng		pcie@17,0 {
60f4b5db7cSBin Meng			#address-cells = <3>;
61f4b5db7cSBin Meng			#size-cells = <2>;
62f4b5db7cSBin Meng			compatible = "pci-bridge";
63f4b5db7cSBin Meng			u-boot,dm-pre-reloc;
64f4b5db7cSBin Meng			reg = <0x0000b800 0x0 0x0 0x0 0x0>;
65f4b5db7cSBin Meng
66f4b5db7cSBin Meng			topcliff@0,0 {
67f4b5db7cSBin Meng				#address-cells = <3>;
68f4b5db7cSBin Meng				#size-cells = <2>;
69f4b5db7cSBin Meng				compatible = "pci-bridge";
70f4b5db7cSBin Meng				u-boot,dm-pre-reloc;
71f4b5db7cSBin Meng				reg = <0x00010000 0x0 0x0 0x0 0x0>;
72f4b5db7cSBin Meng
73f4b5db7cSBin Meng				pciuart0: uart@a,1 {
74f4b5db7cSBin Meng					compatible = "pci8086,8811.00",
75f4b5db7cSBin Meng							"pci8086,8811",
76f4b5db7cSBin Meng							"pciclass,070002",
77f4b5db7cSBin Meng							"pciclass,0700",
78f4b5db7cSBin Meng							"x86-uart";
79f4b5db7cSBin Meng					u-boot,dm-pre-reloc;
80f4b5db7cSBin Meng					reg = <0x00025100 0x0 0x0 0x0 0x0
81f4b5db7cSBin Meng					       0x01025110 0x0 0x0 0x0 0x0>;
82f4b5db7cSBin Meng					......
83f4b5db7cSBin Meng				};
84f4b5db7cSBin Meng
85f4b5db7cSBin Meng				......
86f4b5db7cSBin Meng			};
87f4b5db7cSBin Meng		};
88f4b5db7cSBin Meng
89f4b5db7cSBin Meng		......
90f4b5db7cSBin Meng	};
91f4b5db7cSBin Meng
92f4b5db7cSBin MengIn this example, the root PCI bus node is the "/pci" which matches "pci-x86"
93f4b5db7cSBin Mengdriver. It has a subnode "pcie@17,0" with driver "pci-bridge". "pcie@17,0"
94f4b5db7cSBin Mengalso has subnode "topcliff@0,0" which is a "pci-bridge" too. Under that bridge,
95f4b5db7cSBin Menga PCI UART device "uart@a,1" is described. This exactly reflects the hardware
96f4b5db7cSBin Mengbus hierarchy: on the root PCI bus, there is a PCIe root port which connects
97f4b5db7cSBin Mengto a downstream device Topcliff chipset. Inside Topcliff chipset, it has a
98f4b5db7cSBin MengPCIe-to-PCI bridge and all the chipset integrated devices like the PCI UART
99f4b5db7cSBin Mengdevice are on the PCI bus. Like other devices in the device tree, if we want
100f4b5db7cSBin Mengto bind PCI devices before relocation, "u-boot,dm-pre-reloc" must be declared
101f4b5db7cSBin Mengin each of these nodes.
102f4b5db7cSBin Meng
103f4b5db7cSBin MengIf PCI devices are not listed in the device tree, U_BOOT_PCI_DEVICE can be used
104f4b5db7cSBin Mengto specify the driver to use for the device. The device tree takes precedence
105f4b5db7cSBin Mengover U_BOOT_PCI_DEVICE. Plese note with U_BOOT_PCI_DEVICE, only drivers with
106f4b5db7cSBin MengDM_FLAG_PRE_RELOC will be bound before relocation. If neither device tree nor
107f4b5db7cSBin MengU_BOOT_PCI_DEVICE is provided, the built-in driver (either pci_bridge_drv or
108f4b5db7cSBin Mengpci_generic_drv) will be used.
109ff3e077bSSimon Glass
110ff3e077bSSimon Glass
111ff3e077bSSimon GlassSandbox
112ff3e077bSSimon Glass-------
113ff3e077bSSimon Glass
114ff3e077bSSimon GlassWith sandbox we need a device emulator for each device on the bus since there
115ff3e077bSSimon Glassis no real PCI bus. This works by looking in the device tree node for a
116ff3e077bSSimon Glassdriver. For example:
117ff3e077bSSimon Glass
118ff3e077bSSimon Glass
119ff3e077bSSimon Glass	pci@1f,0 {
120ff3e077bSSimon Glass		compatible = "pci-generic";
121ff3e077bSSimon Glass		reg = <0xf800 0 0 0 0>;
122ff3e077bSSimon Glass		emul@1f,0 {
123ff3e077bSSimon Glass			compatible = "sandbox,swap-case";
124ff3e077bSSimon Glass		};
125ff3e077bSSimon Glass	};
126ff3e077bSSimon Glass
127ff3e077bSSimon GlassThis means that there is a 'sandbox,swap-case' driver at that bus position.
128ff3e077bSSimon GlassNote that the first cell in the 'reg' value is the bus/device/function. See
129ff3e077bSSimon GlassPCI_BDF() for the encoding (it is also specified in the IEEE Std 1275-1994
130ff3e077bSSimon GlassPCI bus binding document, v2.1)
131ff3e077bSSimon Glass
132ff3e077bSSimon GlassWhen this bus is scanned we will end up with something like this:
133ff3e077bSSimon Glass
134ff3e077bSSimon Glass`- * pci-controller @ 05c660c8, 0
135ff3e077bSSimon Glass `-   pci@1f,0 @ 05c661c8, 63488
136ff3e077bSSimon Glass  `-   emul@1f,0 @ 05c662c8
137ff3e077bSSimon Glass
138ff3e077bSSimon GlassWhen accesses go to the pci@1f,0 device they are forwarded to its child, the
139ff3e077bSSimon Glassemulator.
1404345998aSBin Meng
1414345998aSBin MengThe sandbox PCI drivers also support dynamic driver binding, allowing device
1424345998aSBin Mengdriver to declare the driver binding information via U_BOOT_PCI_DEVICE(),
1434345998aSBin Mengeliminating the need to provide any device tree node under the host controller
1444345998aSBin Mengnode. It is required a "sandbox,dev-info" property must be provided in the
1454345998aSBin Menghost controller node for this functionality to work.
1464345998aSBin Meng
1474345998aSBin Meng	pci1: pci-controller1 {
1484345998aSBin Meng		compatible = "sandbox,pci";
1494345998aSBin Meng		...
1504345998aSBin Meng		sandbox,dev-info = <0x08 0x00 0x1234 0x5678
1514345998aSBin Meng				    0x0c 0x00 0x1234 0x5678>;
1524345998aSBin Meng	};
1534345998aSBin Meng
1544345998aSBin MengThe "sandbox,dev-info" property specifies all dynamic PCI devices on this bus.
1554345998aSBin MengEach dynamic PCI device is encoded as 4 cells a group. The first and second
1564345998aSBin Mengcells are PCI device number and function number respectively. The third and
1574345998aSBin Mengfourth cells are PCI vendor ID and device ID respectively.
1584345998aSBin Meng
1594345998aSBin MengWhen this bus is scanned we will end up with something like this:
1604345998aSBin Meng
1614345998aSBin Meng pci        [ + ]   pci_sandbo  |-- pci-controller1
1624345998aSBin Meng pci_emul   [   ]   sandbox_sw  |   |-- sandbox_swap_case_emul
1634345998aSBin Meng pci_emul   [   ]   sandbox_sw  |   `-- sandbox_swap_case_emul
1644345998aSBin Meng
1654345998aSBin MengNote the difference from the statically declared device nodes is that the
1664345998aSBin Mengdevice is directly attached to the host controller, instead of via a container
1674345998aSBin Mengdevice like pci@1f,0.
168