2 * Copyright (c) 2014-2015 François Tigeot
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #define PCI_ANY_ID (~0u)
32 #include <sys/param.h>
34 #include <sys/pciio.h>
36 #include <bus/pci/pcivar.h>
37 #include <bus/pci/pcireg.h>
39 #include <linux/types.h>
40 #include <linux/device.h>
43 #include <linux/pci_ids.h>
47 struct pci_device_id {
54 unsigned long driver_data;
58 struct pci_bus *bus; /* bus device is nailed to */
59 struct device *dev; /* NOTE the star */
61 unsigned short vendor; /* vendor ID */
62 unsigned short device; /* device ID */
63 unsigned short subsystem_vendor;
64 unsigned short subsystem_device;
68 struct pci_dev *self; /* handle to pdev self */
69 struct device *dev; /* handle to dev */
71 unsigned char number; /* bus addr number */
72 // unsigned char max_bus_speed; /* max bus speed */
75 #define PCI_DEVFN(slot, func) ((((slot) & 0x1f) << 3) | ((func) & 0x07))
77 #define PCI_DMA_BIDIRECTIONAL 0
80 pci_read_config_byte(struct pci_dev *pdev, int where, u8 *val)
82 *val = (u16)pci_read_config(pdev->dev, where, 1);
87 pci_read_config_word(struct pci_dev *pdev, int where, u16 *val)
89 *val = (u16)pci_read_config(pdev->dev, where, 2);
94 pci_read_config_dword(struct pci_dev *pdev, int where, u32 *val)
96 *val = (u32)pci_read_config(pdev->dev, where, 4);
101 pci_write_config_byte(struct pci_dev *pdev, int where, u8 val)
103 pci_write_config(pdev->dev, where, val, 1);
108 pci_write_config_word(struct pci_dev *pdev, int where, u16 val)
110 pci_write_config(pdev->dev, where, val, 2);
115 pci_write_config_dword(struct pci_dev *pdev, int where, u32 val)
117 pci_write_config(pdev->dev, where, val, 4);
122 static inline struct pci_dev *
123 pci_dev_get(struct pci_dev *dev)
125 /* Linux increments a reference count here */
129 static inline struct pci_dev *
130 pci_dev_put(struct pci_dev *dev)
132 /* Linux decrements a reference count here */
138 pci_set_dma_mask(struct pci_dev *dev, u64 mask)
144 pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
149 typedef int pci_power_t;
159 static inline struct resource_list_entry*
160 _pci_get_rle(struct pci_dev *pdev, int bar)
162 struct pci_devinfo *dinfo;
163 struct device *dev = pdev->dev;
164 struct resource_list_entry *rle;
166 dinfo = device_get_ivars(dev);
168 /* Some child devices don't have registered resources, they
169 * are only present in the parent */
171 kprintf("_pci_get_rle: dinfo was NULL, trying again with parent\n");
172 dev = device_get_parent(dev);
174 dinfo = device_get_ivars(dev);
178 rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY, PCIR_BAR(bar));
180 rle = resource_list_find(&dinfo->resources,
181 SYS_RES_IOPORT, PCIR_BAR(bar));
188 * Returns the first address (memory address or I/O port number)
189 * associated with one of the PCI I/O regions.The region is selected by
190 * the integer bar (the base address register), ranging from 0–5 (inclusive).
191 * The return value can be used by ioremap()
193 static inline phys_addr_t
194 pci_resource_start(struct pci_dev *pdev, int bar)
196 struct resource_list_entry *rle;
198 rle = _pci_get_rle(pdev, bar);
202 kprintf("pci_resource_start(0x%x, 0x%x) = 0x%lx\n",
203 pdev->device, PCIR_BAR(bar), rman_get_start(rle->res));
205 return rman_get_start(rle->res);
208 static inline phys_addr_t
209 pci_resource_len(struct pci_dev *pdev, int bar)
211 struct resource_list_entry *rle;
213 rle = _pci_get_rle(pdev, bar);
217 kprintf("pci_resource_len(0x%x, 0x%x) = 0x%lx\n",
218 pdev->device, PCIR_BAR(bar), rman_get_size(rle->res));
220 return rman_get_size(rle->res);
223 #endif /* LINUX_PCI_H */