1*4882a593SmuzhiyunPCI with Driver Model 2*4882a593Smuzhiyun===================== 3*4882a593Smuzhiyun 4*4882a593SmuzhiyunHow busses are scanned 5*4882a593Smuzhiyun---------------------- 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunAny config read will end up at pci_read_config(). This uses 8*4882a593Smuzhiyunuclass_get_device_by_seq() to get the PCI bus for a particular bus number. 9*4882a593SmuzhiyunBus number 0 will need to be requested first, and the alias in the device 10*4882a593Smuzhiyuntree file will point to the correct device: 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun 13*4882a593Smuzhiyun aliases { 14*4882a593Smuzhiyun pci0 = &pci; 15*4882a593Smuzhiyun }; 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun pci: pci-controller { 18*4882a593Smuzhiyun compatible = "sandbox,pci"; 19*4882a593Smuzhiyun ... 20*4882a593Smuzhiyun }; 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunIf there is no alias the devices will be numbered sequentially in the device 24*4882a593Smuzhiyuntree. 25*4882a593Smuzhiyun 26*4882a593SmuzhiyunThe call to uclass_get_device() will cause the PCI bus to be probed. 27*4882a593SmuzhiyunThis does a scan of the bus to locate available devices. These devices are 28*4882a593Smuzhiyunbound to their appropriate driver if available. If there is no driver, then 29*4882a593Smuzhiyunthey are bound to a generic PCI driver which does nothing. 30*4882a593Smuzhiyun 31*4882a593SmuzhiyunAfter probing a bus, the available devices will appear in the device tree 32*4882a593Smuzhiyununder that bus. 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunNote that this is all done on a lazy basis, as needed, so until something is 35*4882a593Smuzhiyuntouched on PCI (eg: a call to pci_find_devices()) it will not be probed. 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunPCI devices can appear in the flattened device tree. If they do this serves to 38*4882a593Smuzhiyunspecify the driver to use for the device. In this case they will be bound at 39*4882a593Smuzhiyunfirst. Each PCI device node must have a compatible string list as well as a 40*4882a593Smuzhiyun<reg> property, as defined by the IEEE Std 1275-1994 PCI bus binding document 41*4882a593Smuzhiyunv2.1. Note we must describe PCI devices with the same bus hierarchy as the 42*4882a593Smuzhiyunhardware, otherwise driver model cannot detect the correct parent/children 43*4882a593Smuzhiyunrelationship during PCI bus enumeration thus PCI devices won't be bound to 44*4882a593Smuzhiyuntheir drivers accordingly. A working example like below: 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun pci { 47*4882a593Smuzhiyun #address-cells = <3>; 48*4882a593Smuzhiyun #size-cells = <2>; 49*4882a593Smuzhiyun compatible = "pci-x86"; 50*4882a593Smuzhiyun u-boot,dm-pre-reloc; 51*4882a593Smuzhiyun ranges = <0x02000000 0x0 0x40000000 0x40000000 0 0x80000000 52*4882a593Smuzhiyun 0x42000000 0x0 0xc0000000 0xc0000000 0 0x20000000 53*4882a593Smuzhiyun 0x01000000 0x0 0x2000 0x2000 0 0xe000>; 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun pcie@17,0 { 56*4882a593Smuzhiyun #address-cells = <3>; 57*4882a593Smuzhiyun #size-cells = <2>; 58*4882a593Smuzhiyun compatible = "pci-bridge"; 59*4882a593Smuzhiyun u-boot,dm-pre-reloc; 60*4882a593Smuzhiyun reg = <0x0000b800 0x0 0x0 0x0 0x0>; 61*4882a593Smuzhiyun 62*4882a593Smuzhiyun topcliff@0,0 { 63*4882a593Smuzhiyun #address-cells = <3>; 64*4882a593Smuzhiyun #size-cells = <2>; 65*4882a593Smuzhiyun compatible = "pci-bridge"; 66*4882a593Smuzhiyun u-boot,dm-pre-reloc; 67*4882a593Smuzhiyun reg = <0x00010000 0x0 0x0 0x0 0x0>; 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun pciuart0: uart@a,1 { 70*4882a593Smuzhiyun compatible = "pci8086,8811.00", 71*4882a593Smuzhiyun "pci8086,8811", 72*4882a593Smuzhiyun "pciclass,070002", 73*4882a593Smuzhiyun "pciclass,0700", 74*4882a593Smuzhiyun "x86-uart"; 75*4882a593Smuzhiyun u-boot,dm-pre-reloc; 76*4882a593Smuzhiyun reg = <0x00025100 0x0 0x0 0x0 0x0 77*4882a593Smuzhiyun 0x01025110 0x0 0x0 0x0 0x0>; 78*4882a593Smuzhiyun ...... 79*4882a593Smuzhiyun }; 80*4882a593Smuzhiyun 81*4882a593Smuzhiyun ...... 82*4882a593Smuzhiyun }; 83*4882a593Smuzhiyun }; 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun ...... 86*4882a593Smuzhiyun }; 87*4882a593Smuzhiyun 88*4882a593SmuzhiyunIn this example, the root PCI bus node is the "/pci" which matches "pci-x86" 89*4882a593Smuzhiyundriver. It has a subnode "pcie@17,0" with driver "pci-bridge". "pcie@17,0" 90*4882a593Smuzhiyunalso has subnode "topcliff@0,0" which is a "pci-bridge" too. Under that bridge, 91*4882a593Smuzhiyuna PCI UART device "uart@a,1" is described. This exactly reflects the hardware 92*4882a593Smuzhiyunbus hierarchy: on the root PCI bus, there is a PCIe root port which connects 93*4882a593Smuzhiyunto a downstream device Topcliff chipset. Inside Topcliff chipset, it has a 94*4882a593SmuzhiyunPCIe-to-PCI bridge and all the chipset integrated devices like the PCI UART 95*4882a593Smuzhiyundevice are on the PCI bus. Like other devices in the device tree, if we want 96*4882a593Smuzhiyunto bind PCI devices before relocation, "u-boot,dm-pre-reloc" must be declared 97*4882a593Smuzhiyunin each of these nodes. 98*4882a593Smuzhiyun 99*4882a593SmuzhiyunIf PCI devices are not listed in the device tree, U_BOOT_PCI_DEVICE can be used 100*4882a593Smuzhiyunto specify the driver to use for the device. The device tree takes precedence 101*4882a593Smuzhiyunover U_BOOT_PCI_DEVICE. Plese note with U_BOOT_PCI_DEVICE, only drivers with 102*4882a593SmuzhiyunDM_FLAG_PRE_RELOC will be bound before relocation. If neither device tree nor 103*4882a593SmuzhiyunU_BOOT_PCI_DEVICE is provided, the built-in driver (either pci_bridge_drv or 104*4882a593Smuzhiyunpci_generic_drv) will be used. 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun 107*4882a593SmuzhiyunSandbox 108*4882a593Smuzhiyun------- 109*4882a593Smuzhiyun 110*4882a593SmuzhiyunWith sandbox we need a device emulator for each device on the bus since there 111*4882a593Smuzhiyunis no real PCI bus. This works by looking in the device tree node for a 112*4882a593Smuzhiyundriver. For example: 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun pci@1f,0 { 116*4882a593Smuzhiyun compatible = "pci-generic"; 117*4882a593Smuzhiyun reg = <0xf800 0 0 0 0>; 118*4882a593Smuzhiyun emul@1f,0 { 119*4882a593Smuzhiyun compatible = "sandbox,swap-case"; 120*4882a593Smuzhiyun }; 121*4882a593Smuzhiyun }; 122*4882a593Smuzhiyun 123*4882a593SmuzhiyunThis means that there is a 'sandbox,swap-case' driver at that bus position. 124*4882a593SmuzhiyunNote that the first cell in the 'reg' value is the bus/device/function. See 125*4882a593SmuzhiyunPCI_BDF() for the encoding (it is also specified in the IEEE Std 1275-1994 126*4882a593SmuzhiyunPCI bus binding document, v2.1) 127*4882a593Smuzhiyun 128*4882a593SmuzhiyunWhen this bus is scanned we will end up with something like this: 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun`- * pci-controller @ 05c660c8, 0 131*4882a593Smuzhiyun `- pci@1f,0 @ 05c661c8, 63488 132*4882a593Smuzhiyun `- emul@1f,0 @ 05c662c8 133*4882a593Smuzhiyun 134*4882a593SmuzhiyunWhen accesses go to the pci@1f,0 device they are forwarded to its child, the 135*4882a593Smuzhiyunemulator. 136