1 Few Notes About The PCI Subsystem 2 3 or 4 5 "What should you avoid when writing PCI drivers" 6 7 by Martin Mares <mj@atrey.karlin.mff.cuni.cz> on 13-Feb-1998 8 9~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 10 111. How to find PCI devices 12~~~~~~~~~~~~~~~~~~~~~~~~~~ 13 In case your driver wants to search for all devices with given vendor/device 14ID, it should use: 15 16 struct pci_dev *dev = NULL; 17 while (dev = pci_find_device(VENDOR_ID, DEVICE_ID, dev)) 18 configure_device(dev); 19 20 For class-based search, use pci_find_class(CLASS_ID, dev). 21 22 In case you want to do some complex matching, look at pci_devices -- it's 23a linked list of pci_dev structures for all PCI devices in the system. 24 25 All these methods return a pointer to a pci_dev structure which is used as a 26parameter for many other PCI functions. The rest of them accept bus and 27device/function numbers which can be found in pci_dev->bus->number and 28pci_dev->devfn. Feel free to use all other fields of the pci_dev structure, but 29don't modify them. 30 31 The pci_present() function can be used to test presence of PCI in the 32machine. 33 342. How to access PCI config space 35~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 36 You can use pci_(read|write)_config_(byte|word|dword) to access the config 37space of a device represented by pci_dev. All these functions return 0 when 38successful or an error code (PCIBIOS_...) which can be translated to a text 39string by pcibios_strerror. Most drivers expect that accesses to valid PCI 40devices don't fail. 41 42 In case you want to address the devices by bus/device/function numbers, 43use pcibios_(read_write)_config_(byte|word|dword). 44 45 If you access fields in the standard portion of the config header, please 46use symbolic names of locations and bits declared in <linux/pci.h>. 47 483. Addresses and interrupts 49~~~~~~~~~~~~~~~~~~~~~~~~~~~ 50 Memory and port addresses and interrupt numbers should NOT be read from the 51config space. You should use the values in the pci_dev structure as they might 52have been remapped by the kernel. 53 54 If your PCI device uses PCI I/O space, you need to use the check_region(), 55request_region() and release_region() routines. These prevent devices from 56having conflicting I/O regions. You access your registers using the inb(), 57inw(), inl(), outb(), outw(), or outl() routines passing the value of 58(struct pci_dev *) dev->base_address[] masked by PCI_BASE_ADDRESS_IO_MASK 59as the base address of your registers. 60 61 If your PCI device uses PCI memory space, use ioremap() to create a cookie 62mapping to your PCI device. The mask (struct pci_dev *) dev->base_address[] 63with PCI_BASE_ADDRESS_MEM_MASK before passing it into ioremap(). This cookie 64is passed to the readb(), readw(), readl(), writeb(), writew(), and writel() 65routines when accessing PCI space. You must always use these routines when 66accessing PCI space from the kernel. Not all architectures allow direct access 67to PCI memory space from the kernel. 68 69 The IO-mapping.txt file has information about converting between the 70various address spaces. People writing DMA device drivers should pay special 71attention to this information. 72 73 PCI interrupt routines are always SA_SHIRQ and should use the value from 74(struct pci_dev *) dev->irq field for the interrupt number passed into 75request_irq(). Since it is a shared interrupt, you must also always pass a 76unique dev_id to request_irq(). 77 784. Obsolete functions 79~~~~~~~~~~~~~~~~~~~~~ 80<linux/bios32.h> is obsolete and should not be included in new code. 81 82pcibios_find_(device|class) are also obsolete and should be replaced by 83pci_find_(device|class). 84 855. Bus mastering 86~~~~~~~~~~~~~~~~ 87 If you need to setup a bus-mastering card, just call pci_set_master(). It 88should set PCI_COMMAND_MASTER in the command register and adjust the latency 89timer if needed. 90

