Merge tag 'efi-2020-01-rc2' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / include / pci.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4  * Andreas Heppel <aheppel@sysgo.de>
5  *
6  * (C) Copyright 2002
7  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8  */
9
10 #ifndef _PCI_H
11 #define _PCI_H
12
13 #define PCI_CFG_SPACE_SIZE      256
14 #define PCI_CFG_SPACE_EXP_SIZE  4096
15
16 /*
17  * Under PCI, each device has 256 bytes of configuration address space,
18  * of which the first 64 bytes are standardized as follows:
19  */
20 #define PCI_STD_HEADER_SIZEOF   64
21 #define PCI_VENDOR_ID           0x00    /* 16 bits */
22 #define PCI_DEVICE_ID           0x02    /* 16 bits */
23 #define PCI_COMMAND             0x04    /* 16 bits */
24 #define  PCI_COMMAND_IO         0x1     /* Enable response in I/O space */
25 #define  PCI_COMMAND_MEMORY     0x2     /* Enable response in Memory space */
26 #define  PCI_COMMAND_MASTER     0x4     /* Enable bus mastering */
27 #define  PCI_COMMAND_SPECIAL    0x8     /* Enable response to special cycles */
28 #define  PCI_COMMAND_INVALIDATE 0x10    /* Use memory write and invalidate */
29 #define  PCI_COMMAND_VGA_PALETTE 0x20   /* Enable palette snooping */
30 #define  PCI_COMMAND_PARITY     0x40    /* Enable parity checking */
31 #define  PCI_COMMAND_WAIT       0x80    /* Enable address/data stepping */
32 #define  PCI_COMMAND_SERR       0x100   /* Enable SERR */
33 #define  PCI_COMMAND_FAST_BACK  0x200   /* Enable back-to-back writes */
34
35 #define PCI_STATUS              0x06    /* 16 bits */
36 #define  PCI_STATUS_CAP_LIST    0x10    /* Support Capability List */
37 #define  PCI_STATUS_66MHZ       0x20    /* Support 66 Mhz PCI 2.1 bus */
38 #define  PCI_STATUS_UDF         0x40    /* Support User Definable Features [obsolete] */
39 #define  PCI_STATUS_FAST_BACK   0x80    /* Accept fast-back to back */
40 #define  PCI_STATUS_PARITY      0x100   /* Detected parity error */
41 #define  PCI_STATUS_DEVSEL_MASK 0x600   /* DEVSEL timing */
42 #define  PCI_STATUS_DEVSEL_FAST 0x000
43 #define  PCI_STATUS_DEVSEL_MEDIUM 0x200
44 #define  PCI_STATUS_DEVSEL_SLOW 0x400
45 #define  PCI_STATUS_SIG_TARGET_ABORT 0x800 /* Set on target abort */
46 #define  PCI_STATUS_REC_TARGET_ABORT 0x1000 /* Master ack of " */
47 #define  PCI_STATUS_REC_MASTER_ABORT 0x2000 /* Set on master abort */
48 #define  PCI_STATUS_SIG_SYSTEM_ERROR 0x4000 /* Set when we drive SERR */
49 #define  PCI_STATUS_DETECTED_PARITY 0x8000 /* Set on parity error */
50
51 #define PCI_CLASS_REVISION      0x08    /* High 24 bits are class, low 8
52                                            revision */
53 #define PCI_REVISION_ID         0x08    /* Revision ID */
54 #define PCI_CLASS_PROG          0x09    /* Reg. Level Programming Interface */
55 #define PCI_CLASS_DEVICE        0x0a    /* Device class */
56 #define PCI_CLASS_CODE          0x0b    /* Device class code */
57 #define  PCI_CLASS_CODE_TOO_OLD 0x00
58 #define  PCI_CLASS_CODE_STORAGE 0x01
59 #define  PCI_CLASS_CODE_NETWORK 0x02
60 #define  PCI_CLASS_CODE_DISPLAY 0x03
61 #define  PCI_CLASS_CODE_MULTIMEDIA 0x04
62 #define  PCI_CLASS_CODE_MEMORY  0x05
63 #define  PCI_CLASS_CODE_BRIDGE  0x06
64 #define  PCI_CLASS_CODE_COMM    0x07
65 #define  PCI_CLASS_CODE_PERIPHERAL 0x08
66 #define  PCI_CLASS_CODE_INPUT   0x09
67 #define  PCI_CLASS_CODE_DOCKING 0x0A
68 #define  PCI_CLASS_CODE_PROCESSOR 0x0B
69 #define  PCI_CLASS_CODE_SERIAL  0x0C
70 #define  PCI_CLASS_CODE_WIRELESS 0x0D
71 #define  PCI_CLASS_CODE_I2O     0x0E
72 #define  PCI_CLASS_CODE_SATELLITE 0x0F
73 #define  PCI_CLASS_CODE_CRYPTO  0x10
74 #define  PCI_CLASS_CODE_DATA    0x11
75 /* Base Class 0x12 - 0xFE is reserved */
76 #define  PCI_CLASS_CODE_OTHER   0xFF
77
78 #define PCI_CLASS_SUB_CODE      0x0a    /* Device sub-class code */
79 #define  PCI_CLASS_SUB_CODE_TOO_OLD_NOTVGA      0x00
80 #define  PCI_CLASS_SUB_CODE_TOO_OLD_VGA         0x01
81 #define  PCI_CLASS_SUB_CODE_STORAGE_SCSI        0x00
82 #define  PCI_CLASS_SUB_CODE_STORAGE_IDE         0x01
83 #define  PCI_CLASS_SUB_CODE_STORAGE_FLOPPY      0x02
84 #define  PCI_CLASS_SUB_CODE_STORAGE_IPIBUS      0x03
85 #define  PCI_CLASS_SUB_CODE_STORAGE_RAID        0x04
86 #define  PCI_CLASS_SUB_CODE_STORAGE_ATA         0x05
87 #define  PCI_CLASS_SUB_CODE_STORAGE_SATA        0x06
88 #define  PCI_CLASS_SUB_CODE_STORAGE_SAS         0x07
89 #define  PCI_CLASS_SUB_CODE_STORAGE_OTHER       0x80
90 #define  PCI_CLASS_SUB_CODE_NETWORK_ETHERNET    0x00
91 #define  PCI_CLASS_SUB_CODE_NETWORK_TOKENRING   0x01
92 #define  PCI_CLASS_SUB_CODE_NETWORK_FDDI        0x02
93 #define  PCI_CLASS_SUB_CODE_NETWORK_ATM         0x03
94 #define  PCI_CLASS_SUB_CODE_NETWORK_ISDN        0x04
95 #define  PCI_CLASS_SUB_CODE_NETWORK_WORLDFIP    0x05
96 #define  PCI_CLASS_SUB_CODE_NETWORK_PICMG       0x06
97 #define  PCI_CLASS_SUB_CODE_NETWORK_OTHER       0x80
98 #define  PCI_CLASS_SUB_CODE_DISPLAY_VGA         0x00
99 #define  PCI_CLASS_SUB_CODE_DISPLAY_XGA         0x01
100 #define  PCI_CLASS_SUB_CODE_DISPLAY_3D          0x02
101 #define  PCI_CLASS_SUB_CODE_DISPLAY_OTHER       0x80
102 #define  PCI_CLASS_SUB_CODE_MULTIMEDIA_VIDEO    0x00
103 #define  PCI_CLASS_SUB_CODE_MULTIMEDIA_AUDIO    0x01
104 #define  PCI_CLASS_SUB_CODE_MULTIMEDIA_PHONE    0x02
105 #define  PCI_CLASS_SUB_CODE_MULTIMEDIA_OTHER    0x80
106 #define  PCI_CLASS_SUB_CODE_MEMORY_RAM          0x00
107 #define  PCI_CLASS_SUB_CODE_MEMORY_FLASH        0x01
108 #define  PCI_CLASS_SUB_CODE_MEMORY_OTHER        0x80
109 #define  PCI_CLASS_SUB_CODE_BRIDGE_HOST         0x00
110 #define  PCI_CLASS_SUB_CODE_BRIDGE_ISA          0x01
111 #define  PCI_CLASS_SUB_CODE_BRIDGE_EISA         0x02
112 #define  PCI_CLASS_SUB_CODE_BRIDGE_MCA          0x03
113 #define  PCI_CLASS_SUB_CODE_BRIDGE_PCI          0x04
114 #define  PCI_CLASS_SUB_CODE_BRIDGE_PCMCIA       0x05
115 #define  PCI_CLASS_SUB_CODE_BRIDGE_NUBUS        0x06
116 #define  PCI_CLASS_SUB_CODE_BRIDGE_CARDBUS      0x07
117 #define  PCI_CLASS_SUB_CODE_BRIDGE_RACEWAY      0x08
118 #define  PCI_CLASS_SUB_CODE_BRIDGE_SEMI_PCI     0x09
119 #define  PCI_CLASS_SUB_CODE_BRIDGE_INFINIBAND   0x0A
120 #define  PCI_CLASS_SUB_CODE_BRIDGE_OTHER        0x80
121 #define  PCI_CLASS_SUB_CODE_COMM_SERIAL         0x00
122 #define  PCI_CLASS_SUB_CODE_COMM_PARALLEL       0x01
123 #define  PCI_CLASS_SUB_CODE_COMM_MULTIPORT      0x02
124 #define  PCI_CLASS_SUB_CODE_COMM_MODEM          0x03
125 #define  PCI_CLASS_SUB_CODE_COMM_GPIB           0x04
126 #define  PCI_CLASS_SUB_CODE_COMM_SMARTCARD      0x05
127 #define  PCI_CLASS_SUB_CODE_COMM_OTHER          0x80
128 #define  PCI_CLASS_SUB_CODE_PERIPHERAL_PIC      0x00
129 #define  PCI_CLASS_SUB_CODE_PERIPHERAL_DMA      0x01
130 #define  PCI_CLASS_SUB_CODE_PERIPHERAL_TIMER    0x02
131 #define  PCI_CLASS_SUB_CODE_PERIPHERAL_RTC      0x03
132 #define  PCI_CLASS_SUB_CODE_PERIPHERAL_HOTPLUG  0x04
133 #define  PCI_CLASS_SUB_CODE_PERIPHERAL_SD       0x05
134 #define  PCI_CLASS_SUB_CODE_PERIPHERAL_OTHER    0x80
135 #define  PCI_CLASS_SUB_CODE_INPUT_KEYBOARD      0x00
136 #define  PCI_CLASS_SUB_CODE_INPUT_DIGITIZER     0x01
137 #define  PCI_CLASS_SUB_CODE_INPUT_MOUSE         0x02
138 #define  PCI_CLASS_SUB_CODE_INPUT_SCANNER       0x03
139 #define  PCI_CLASS_SUB_CODE_INPUT_GAMEPORT      0x04
140 #define  PCI_CLASS_SUB_CODE_INPUT_OTHER         0x80
141 #define  PCI_CLASS_SUB_CODE_DOCKING_GENERIC     0x00
142 #define  PCI_CLASS_SUB_CODE_DOCKING_OTHER       0x80
143 #define  PCI_CLASS_SUB_CODE_PROCESSOR_386       0x00
144 #define  PCI_CLASS_SUB_CODE_PROCESSOR_486       0x01
145 #define  PCI_CLASS_SUB_CODE_PROCESSOR_PENTIUM   0x02
146 #define  PCI_CLASS_SUB_CODE_PROCESSOR_ALPHA     0x10
147 #define  PCI_CLASS_SUB_CODE_PROCESSOR_POWERPC   0x20
148 #define  PCI_CLASS_SUB_CODE_PROCESSOR_MIPS      0x30
149 #define  PCI_CLASS_SUB_CODE_PROCESSOR_COPROC    0x40
150 #define  PCI_CLASS_SUB_CODE_SERIAL_1394         0x00
151 #define  PCI_CLASS_SUB_CODE_SERIAL_ACCESSBUS    0x01
152 #define  PCI_CLASS_SUB_CODE_SERIAL_SSA          0x02
153 #define  PCI_CLASS_SUB_CODE_SERIAL_USB          0x03
154 #define  PCI_CLASS_SUB_CODE_SERIAL_FIBRECHAN    0x04
155 #define  PCI_CLASS_SUB_CODE_SERIAL_SMBUS        0x05
156 #define  PCI_CLASS_SUB_CODE_SERIAL_INFINIBAND   0x06
157 #define  PCI_CLASS_SUB_CODE_SERIAL_IPMI         0x07
158 #define  PCI_CLASS_SUB_CODE_SERIAL_SERCOS       0x08
159 #define  PCI_CLASS_SUB_CODE_SERIAL_CANBUS       0x09
160 #define  PCI_CLASS_SUB_CODE_WIRELESS_IRDA       0x00
161 #define  PCI_CLASS_SUB_CODE_WIRELESS_IR         0x01
162 #define  PCI_CLASS_SUB_CODE_WIRELESS_RF         0x10
163 #define  PCI_CLASS_SUB_CODE_WIRELESS_BLUETOOTH  0x11
164 #define  PCI_CLASS_SUB_CODE_WIRELESS_BROADBAND  0x12
165 #define  PCI_CLASS_SUB_CODE_WIRELESS_80211A     0x20
166 #define  PCI_CLASS_SUB_CODE_WIRELESS_80211B     0x21
167 #define  PCI_CLASS_SUB_CODE_WIRELESS_OTHER      0x80
168 #define  PCI_CLASS_SUB_CODE_I2O_V1_0            0x00
169 #define  PCI_CLASS_SUB_CODE_SATELLITE_TV        0x01
170 #define  PCI_CLASS_SUB_CODE_SATELLITE_AUDIO     0x02
171 #define  PCI_CLASS_SUB_CODE_SATELLITE_VOICE     0x03
172 #define  PCI_CLASS_SUB_CODE_SATELLITE_DATA      0x04
173 #define  PCI_CLASS_SUB_CODE_CRYPTO_NETWORK      0x00
174 #define  PCI_CLASS_SUB_CODE_CRYPTO_ENTERTAINMENT 0x10
175 #define  PCI_CLASS_SUB_CODE_CRYPTO_OTHER        0x80
176 #define  PCI_CLASS_SUB_CODE_DATA_DPIO           0x00
177 #define  PCI_CLASS_SUB_CODE_DATA_PERFCNTR       0x01
178 #define  PCI_CLASS_SUB_CODE_DATA_COMMSYNC       0x10
179 #define  PCI_CLASS_SUB_CODE_DATA_MGMT           0x20
180 #define  PCI_CLASS_SUB_CODE_DATA_OTHER          0x80
181
182 #define PCI_CACHE_LINE_SIZE     0x0c    /* 8 bits */
183 #define PCI_LATENCY_TIMER       0x0d    /* 8 bits */
184 #define PCI_HEADER_TYPE         0x0e    /* 8 bits */
185 #define  PCI_HEADER_TYPE_NORMAL 0
186 #define  PCI_HEADER_TYPE_BRIDGE 1
187 #define  PCI_HEADER_TYPE_CARDBUS 2
188
189 #define PCI_BIST                0x0f    /* 8 bits */
190 #define PCI_BIST_CODE_MASK      0x0f    /* Return result */
191 #define PCI_BIST_START          0x40    /* 1 to start BIST, 2 secs or less */
192 #define PCI_BIST_CAPABLE        0x80    /* 1 if BIST capable */
193
194 /*
195  * Base addresses specify locations in memory or I/O space.
196  * Decoded size can be determined by writing a value of
197  * 0xffffffff to the register, and reading it back.  Only
198  * 1 bits are decoded.
199  */
200 #define PCI_BASE_ADDRESS_0      0x10    /* 32 bits */
201 #define PCI_BASE_ADDRESS_1      0x14    /* 32 bits [htype 0,1 only] */
202 #define PCI_BASE_ADDRESS_2      0x18    /* 32 bits [htype 0 only] */
203 #define PCI_BASE_ADDRESS_3      0x1c    /* 32 bits */
204 #define PCI_BASE_ADDRESS_4      0x20    /* 32 bits */
205 #define PCI_BASE_ADDRESS_5      0x24    /* 32 bits */
206 #define  PCI_BASE_ADDRESS_SPACE 0x01    /* 0 = memory, 1 = I/O */
207 #define  PCI_BASE_ADDRESS_SPACE_IO 0x01
208 #define  PCI_BASE_ADDRESS_SPACE_MEMORY 0x00
209 #define  PCI_BASE_ADDRESS_MEM_TYPE_MASK 0x06
210 #define  PCI_BASE_ADDRESS_MEM_TYPE_32   0x00    /* 32 bit address */
211 #define  PCI_BASE_ADDRESS_MEM_TYPE_1M   0x02    /* Below 1M [obsolete] */
212 #define  PCI_BASE_ADDRESS_MEM_TYPE_64   0x04    /* 64 bit address */
213 #define  PCI_BASE_ADDRESS_MEM_PREFETCH  0x08    /* prefetchable? */
214 #define  PCI_BASE_ADDRESS_MEM_MASK      (~0x0fULL)
215 #define  PCI_BASE_ADDRESS_IO_MASK       (~0x03ULL)
216 /* bit 1 is reserved if address_space = 1 */
217
218 /* Convert a regsister address (e.g. PCI_BASE_ADDRESS_1) to a bar # (e.g. 1) */
219 #define pci_offset_to_barnum(offset)    \
220                 (((offset) - PCI_BASE_ADDRESS_0) / sizeof(u32))
221
222 /* Header type 0 (normal devices) */
223 #define PCI_CARDBUS_CIS         0x28
224 #define PCI_SUBSYSTEM_VENDOR_ID 0x2c
225 #define PCI_SUBSYSTEM_ID        0x2e
226 #define PCI_ROM_ADDRESS         0x30    /* Bits 31..11 are address, 10..1 reserved */
227 #define  PCI_ROM_ADDRESS_ENABLE 0x01
228 #define PCI_ROM_ADDRESS_MASK    (~0x7ffULL)
229
230 #define PCI_CAPABILITY_LIST     0x34    /* Offset of first capability list entry */
231
232 /* 0x35-0x3b are reserved */
233 #define PCI_INTERRUPT_LINE      0x3c    /* 8 bits */
234 #define PCI_INTERRUPT_PIN       0x3d    /* 8 bits */
235 #define PCI_MIN_GNT             0x3e    /* 8 bits */
236 #define PCI_MAX_LAT             0x3f    /* 8 bits */
237
238 #define PCI_INTERRUPT_LINE_DISABLE      0xff
239
240 /* Header type 1 (PCI-to-PCI bridges) */
241 #define PCI_PRIMARY_BUS         0x18    /* Primary bus number */
242 #define PCI_SECONDARY_BUS       0x19    /* Secondary bus number */
243 #define PCI_SUBORDINATE_BUS     0x1a    /* Highest bus number behind the bridge */
244 #define PCI_SEC_LATENCY_TIMER   0x1b    /* Latency timer for secondary interface */
245 #define PCI_IO_BASE             0x1c    /* I/O range behind the bridge */
246 #define PCI_IO_LIMIT            0x1d
247 #define  PCI_IO_RANGE_TYPE_MASK 0x0f    /* I/O bridging type */
248 #define  PCI_IO_RANGE_TYPE_16   0x00
249 #define  PCI_IO_RANGE_TYPE_32   0x01
250 #define  PCI_IO_RANGE_MASK      ~0x0f
251 #define PCI_SEC_STATUS          0x1e    /* Secondary status register, only bit 14 used */
252 #define PCI_MEMORY_BASE         0x20    /* Memory range behind */
253 #define PCI_MEMORY_LIMIT        0x22
254 #define  PCI_MEMORY_RANGE_TYPE_MASK 0x0f
255 #define  PCI_MEMORY_RANGE_MASK  ~0x0f
256 #define PCI_PREF_MEMORY_BASE    0x24    /* Prefetchable memory range behind */
257 #define PCI_PREF_MEMORY_LIMIT   0x26
258 #define  PCI_PREF_RANGE_TYPE_MASK 0x0f
259 #define  PCI_PREF_RANGE_TYPE_32 0x00
260 #define  PCI_PREF_RANGE_TYPE_64 0x01
261 #define  PCI_PREF_RANGE_MASK    ~0x0f
262 #define PCI_PREF_BASE_UPPER32   0x28    /* Upper half of prefetchable memory range */
263 #define PCI_PREF_LIMIT_UPPER32  0x2c
264 #define PCI_IO_BASE_UPPER16     0x30    /* Upper half of I/O addresses */
265 #define PCI_IO_LIMIT_UPPER16    0x32
266 /* 0x34 same as for htype 0 */
267 /* 0x35-0x3b is reserved */
268 #define PCI_ROM_ADDRESS1        0x38    /* Same as PCI_ROM_ADDRESS, but for htype 1 */
269 /* 0x3c-0x3d are same as for htype 0 */
270 #define PCI_BRIDGE_CONTROL      0x3e
271 #define  PCI_BRIDGE_CTL_PARITY  0x01    /* Enable parity detection on secondary interface */
272 #define  PCI_BRIDGE_CTL_SERR    0x02    /* The same for SERR forwarding */
273 #define  PCI_BRIDGE_CTL_NO_ISA  0x04    /* Disable bridging of ISA ports */
274 #define  PCI_BRIDGE_CTL_VGA     0x08    /* Forward VGA addresses */
275 #define  PCI_BRIDGE_CTL_MASTER_ABORT 0x20  /* Report master aborts */
276 #define  PCI_BRIDGE_CTL_BUS_RESET 0x40  /* Secondary bus reset */
277 #define  PCI_BRIDGE_CTL_FAST_BACK 0x80  /* Fast Back2Back enabled on secondary interface */
278
279 /* Header type 2 (CardBus bridges) */
280 #define PCI_CB_CAPABILITY_LIST  0x14
281 /* 0x15 reserved */
282 #define PCI_CB_SEC_STATUS       0x16    /* Secondary status */
283 #define PCI_CB_PRIMARY_BUS      0x18    /* PCI bus number */
284 #define PCI_CB_CARD_BUS         0x19    /* CardBus bus number */
285 #define PCI_CB_SUBORDINATE_BUS  0x1a    /* Subordinate bus number */
286 #define PCI_CB_LATENCY_TIMER    0x1b    /* CardBus latency timer */
287 #define PCI_CB_MEMORY_BASE_0    0x1c
288 #define PCI_CB_MEMORY_LIMIT_0   0x20
289 #define PCI_CB_MEMORY_BASE_1    0x24
290 #define PCI_CB_MEMORY_LIMIT_1   0x28
291 #define PCI_CB_IO_BASE_0        0x2c
292 #define PCI_CB_IO_BASE_0_HI     0x2e
293 #define PCI_CB_IO_LIMIT_0       0x30
294 #define PCI_CB_IO_LIMIT_0_HI    0x32
295 #define PCI_CB_IO_BASE_1        0x34
296 #define PCI_CB_IO_BASE_1_HI     0x36
297 #define PCI_CB_IO_LIMIT_1       0x38
298 #define PCI_CB_IO_LIMIT_1_HI    0x3a
299 #define  PCI_CB_IO_RANGE_MASK   ~0x03
300 /* 0x3c-0x3d are same as for htype 0 */
301 #define PCI_CB_BRIDGE_CONTROL   0x3e
302 #define  PCI_CB_BRIDGE_CTL_PARITY       0x01    /* Similar to standard bridge control register */
303 #define  PCI_CB_BRIDGE_CTL_SERR         0x02
304 #define  PCI_CB_BRIDGE_CTL_ISA          0x04
305 #define  PCI_CB_BRIDGE_CTL_VGA          0x08
306 #define  PCI_CB_BRIDGE_CTL_MASTER_ABORT 0x20
307 #define  PCI_CB_BRIDGE_CTL_CB_RESET     0x40    /* CardBus reset */
308 #define  PCI_CB_BRIDGE_CTL_16BIT_INT    0x80    /* Enable interrupt for 16-bit cards */
309 #define  PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 0x100  /* Prefetch enable for both memory regions */
310 #define  PCI_CB_BRIDGE_CTL_PREFETCH_MEM1 0x200
311 #define  PCI_CB_BRIDGE_CTL_POST_WRITES  0x400
312 #define PCI_CB_SUBSYSTEM_VENDOR_ID 0x40
313 #define PCI_CB_SUBSYSTEM_ID     0x42
314 #define PCI_CB_LEGACY_MODE_BASE 0x44    /* 16-bit PC Card legacy mode base address (ExCa) */
315 /* 0x48-0x7f reserved */
316
317 /* Capability lists */
318
319 #define PCI_CAP_LIST_ID         0       /* Capability ID */
320 #define  PCI_CAP_ID_PM          0x01    /* Power Management */
321 #define  PCI_CAP_ID_AGP         0x02    /* Accelerated Graphics Port */
322 #define  PCI_CAP_ID_VPD         0x03    /* Vital Product Data */
323 #define  PCI_CAP_ID_SLOTID      0x04    /* Slot Identification */
324 #define  PCI_CAP_ID_MSI         0x05    /* Message Signalled Interrupts */
325 #define  PCI_CAP_ID_CHSWP       0x06    /* CompactPCI HotSwap */
326 #define  PCI_CAP_ID_PCIX        0x07    /* PCI-X */
327 #define  PCI_CAP_ID_HT          0x08    /* HyperTransport */
328 #define  PCI_CAP_ID_VNDR        0x09    /* Vendor-Specific */
329 #define  PCI_CAP_ID_DBG         0x0A    /* Debug port */
330 #define  PCI_CAP_ID_CCRC        0x0B    /* CompactPCI Central Resource Control */
331 #define  PCI_CAP_ID_SHPC        0x0C    /* PCI Standard Hot-Plug Controller */
332 #define  PCI_CAP_ID_SSVID       0x0D    /* Bridge subsystem vendor/device ID */
333 #define  PCI_CAP_ID_AGP3        0x0E    /* AGP Target PCI-PCI bridge */
334 #define  PCI_CAP_ID_SECDEV      0x0F    /* Secure Device */
335 #define  PCI_CAP_ID_EXP         0x10    /* PCI Express */
336 #define  PCI_CAP_ID_MSIX        0x11    /* MSI-X */
337 #define  PCI_CAP_ID_SATA        0x12    /* SATA Data/Index Conf. */
338 #define  PCI_CAP_ID_AF          0x13    /* PCI Advanced Features */
339 #define  PCI_CAP_ID_EA          0x14    /* PCI Enhanced Allocation */
340 #define  PCI_CAP_ID_MAX         PCI_CAP_ID_EA
341 #define PCI_CAP_LIST_NEXT       1       /* Next capability in the list */
342 #define PCI_CAP_FLAGS           2       /* Capability defined flags (16 bits) */
343 #define PCI_CAP_SIZEOF          4
344
345 /* Power Management Registers */
346
347 #define  PCI_PM_CAP_VER_MASK    0x0007  /* Version */
348 #define  PCI_PM_CAP_PME_CLOCK   0x0008  /* PME clock required */
349 #define  PCI_PM_CAP_AUX_POWER   0x0010  /* Auxilliary power support */
350 #define  PCI_PM_CAP_DSI         0x0020  /* Device specific initialization */
351 #define  PCI_PM_CAP_D1          0x0200  /* D1 power state support */
352 #define  PCI_PM_CAP_D2          0x0400  /* D2 power state support */
353 #define  PCI_PM_CAP_PME         0x0800  /* PME pin supported */
354 #define PCI_PM_CTRL             4       /* PM control and status register */
355 #define  PCI_PM_CTRL_STATE_MASK 0x0003  /* Current power state (D0 to D3) */
356 #define  PCI_PM_CTRL_PME_ENABLE 0x0100  /* PME pin enable */
357 #define  PCI_PM_CTRL_DATA_SEL_MASK      0x1e00  /* Data select (??) */
358 #define  PCI_PM_CTRL_DATA_SCALE_MASK    0x6000  /* Data scale (??) */
359 #define  PCI_PM_CTRL_PME_STATUS 0x8000  /* PME pin status */
360 #define PCI_PM_PPB_EXTENSIONS   6       /* PPB support extensions (??) */
361 #define  PCI_PM_PPB_B2_B3       0x40    /* Stop clock when in D3hot (??) */
362 #define  PCI_PM_BPCC_ENABLE     0x80    /* Bus power/clock control enable (??) */
363 #define PCI_PM_DATA_REGISTER    7       /* (??) */
364 #define PCI_PM_SIZEOF           8
365
366 /* AGP registers */
367
368 #define PCI_AGP_VERSION         2       /* BCD version number */
369 #define PCI_AGP_RFU             3       /* Rest of capability flags */
370 #define PCI_AGP_STATUS          4       /* Status register */
371 #define  PCI_AGP_STATUS_RQ_MASK 0xff000000      /* Maximum number of requests - 1 */
372 #define  PCI_AGP_STATUS_SBA     0x0200  /* Sideband addressing supported */
373 #define  PCI_AGP_STATUS_64BIT   0x0020  /* 64-bit addressing supported */
374 #define  PCI_AGP_STATUS_FW      0x0010  /* FW transfers supported */
375 #define  PCI_AGP_STATUS_RATE4   0x0004  /* 4x transfer rate supported */
376 #define  PCI_AGP_STATUS_RATE2   0x0002  /* 2x transfer rate supported */
377 #define  PCI_AGP_STATUS_RATE1   0x0001  /* 1x transfer rate supported */
378 #define PCI_AGP_COMMAND         8       /* Control register */
379 #define  PCI_AGP_COMMAND_RQ_MASK 0xff000000  /* Master: Maximum number of requests */
380 #define  PCI_AGP_COMMAND_SBA    0x0200  /* Sideband addressing enabled */
381 #define  PCI_AGP_COMMAND_AGP    0x0100  /* Allow processing of AGP transactions */
382 #define  PCI_AGP_COMMAND_64BIT  0x0020  /* Allow processing of 64-bit addresses */
383 #define  PCI_AGP_COMMAND_FW     0x0010  /* Force FW transfers */
384 #define  PCI_AGP_COMMAND_RATE4  0x0004  /* Use 4x rate */
385 #define  PCI_AGP_COMMAND_RATE2  0x0002  /* Use 4x rate */
386 #define  PCI_AGP_COMMAND_RATE1  0x0001  /* Use 4x rate */
387 #define PCI_AGP_SIZEOF          12
388
389 /* PCI-X registers */
390
391 #define  PCI_X_CMD_DPERR_E      0x0001  /* Data Parity Error Recovery Enable */
392 #define  PCI_X_CMD_ERO          0x0002  /* Enable Relaxed Ordering */
393 #define  PCI_X_CMD_MAX_READ     0x0000  /* Max Memory Read Byte Count */
394 #define  PCI_X_CMD_MAX_SPLIT    0x0030  /* Max Outstanding Split Transactions */
395 #define  PCI_X_CMD_VERSION(x)   (((x) >> 12) & 3) /* Version */
396
397
398 /* Slot Identification */
399
400 #define PCI_SID_ESR             2       /* Expansion Slot Register */
401 #define  PCI_SID_ESR_NSLOTS     0x1f    /* Number of expansion slots available */
402 #define  PCI_SID_ESR_FIC        0x20    /* First In Chassis Flag */
403 #define PCI_SID_CHASSIS_NR      3       /* Chassis Number */
404
405 /* Message Signalled Interrupts registers */
406
407 #define PCI_MSI_FLAGS           2       /* Various flags */
408 #define  PCI_MSI_FLAGS_64BIT    0x80    /* 64-bit addresses allowed */
409 #define  PCI_MSI_FLAGS_QSIZE    0x70    /* Message queue size configured */
410 #define  PCI_MSI_FLAGS_QMASK    0x0e    /* Maximum queue size available */
411 #define  PCI_MSI_FLAGS_ENABLE   0x01    /* MSI feature enabled */
412 #define  PCI_MSI_FLAGS_MASKBIT  0x0100  /* Per-vector masking capable */
413 #define PCI_MSI_RFU             3       /* Rest of capability flags */
414 #define PCI_MSI_ADDRESS_LO      4       /* Lower 32 bits */
415 #define PCI_MSI_ADDRESS_HI      8       /* Upper 32 bits (if PCI_MSI_FLAGS_64BIT set) */
416 #define PCI_MSI_DATA_32         8       /* 16 bits of data for 32-bit devices */
417 #define PCI_MSI_DATA_64         12      /* 16 bits of data for 64-bit devices */
418
419 #define PCI_MAX_PCI_DEVICES     32
420 #define PCI_MAX_PCI_FUNCTIONS   8
421
422 #define PCI_FIND_CAP_TTL 0x48
423 #define CAP_START_POS 0x40
424
425 /* Extended Capabilities (PCI-X 2.0 and Express) */
426 #define PCI_EXT_CAP_ID(header)          (header & 0x0000ffff)
427 #define PCI_EXT_CAP_VER(header)         ((header >> 16) & 0xf)
428 #define PCI_EXT_CAP_NEXT(header)        ((header >> 20) & 0xffc)
429
430 #define PCI_EXT_CAP_ID_ERR      0x01    /* Advanced Error Reporting */
431 #define PCI_EXT_CAP_ID_VC       0x02    /* Virtual Channel Capability */
432 #define PCI_EXT_CAP_ID_DSN      0x03    /* Device Serial Number */
433 #define PCI_EXT_CAP_ID_PWR      0x04    /* Power Budgeting */
434 #define PCI_EXT_CAP_ID_RCLD     0x05    /* Root Complex Link Declaration */
435 #define PCI_EXT_CAP_ID_RCILC    0x06    /* Root Complex Internal Link Control */
436 #define PCI_EXT_CAP_ID_RCEC     0x07    /* Root Complex Event Collector */
437 #define PCI_EXT_CAP_ID_MFVC     0x08    /* Multi-Function VC Capability */
438 #define PCI_EXT_CAP_ID_VC9      0x09    /* same as _VC */
439 #define PCI_EXT_CAP_ID_RCRB     0x0A    /* Root Complex RB? */
440 #define PCI_EXT_CAP_ID_VNDR     0x0B    /* Vendor-Specific */
441 #define PCI_EXT_CAP_ID_CAC      0x0C    /* Config Access - obsolete */
442 #define PCI_EXT_CAP_ID_ACS      0x0D    /* Access Control Services */
443 #define PCI_EXT_CAP_ID_ARI      0x0E    /* Alternate Routing ID */
444 #define PCI_EXT_CAP_ID_ATS      0x0F    /* Address Translation Services */
445 #define PCI_EXT_CAP_ID_SRIOV    0x10    /* Single Root I/O Virtualization */
446 #define PCI_EXT_CAP_ID_MRIOV    0x11    /* Multi Root I/O Virtualization */
447 #define PCI_EXT_CAP_ID_MCAST    0x12    /* Multicast */
448 #define PCI_EXT_CAP_ID_PRI      0x13    /* Page Request Interface */
449 #define PCI_EXT_CAP_ID_AMD_XXX  0x14    /* Reserved for AMD */
450 #define PCI_EXT_CAP_ID_REBAR    0x15    /* Resizable BAR */
451 #define PCI_EXT_CAP_ID_DPA      0x16    /* Dynamic Power Allocation */
452 #define PCI_EXT_CAP_ID_TPH      0x17    /* TPH Requester */
453 #define PCI_EXT_CAP_ID_LTR      0x18    /* Latency Tolerance Reporting */
454 #define PCI_EXT_CAP_ID_SECPCI   0x19    /* Secondary PCIe Capability */
455 #define PCI_EXT_CAP_ID_PMUX     0x1A    /* Protocol Multiplexing */
456 #define PCI_EXT_CAP_ID_PASID    0x1B    /* Process Address Space ID */
457 #define PCI_EXT_CAP_ID_DPC      0x1D    /* Downstream Port Containment */
458 #define PCI_EXT_CAP_ID_L1SS     0x1E    /* L1 PM Substates */
459 #define PCI_EXT_CAP_ID_PTM      0x1F    /* Precision Time Measurement */
460 #define PCI_EXT_CAP_ID_MAX      PCI_EXT_CAP_ID_PTM
461
462 /* Enhanced Allocation Registers */
463 #define PCI_EA_NUM_ENT          2       /* Number of Capability Entries */
464 #define  PCI_EA_NUM_ENT_MASK    0x3f    /* Num Entries Mask */
465 #define PCI_EA_FIRST_ENT        4       /* First EA Entry in List */
466 #define  PCI_EA_ES              0x00000007 /* Entry Size */
467 #define  PCI_EA_BEI             0x000000f0 /* BAR Equivalent Indicator */
468 /* Base, MaxOffset registers */
469 /* bit 0 is reserved */
470 #define  PCI_EA_IS_64           0x00000002      /* 64-bit field flag */
471 #define  PCI_EA_FIELD_MASK      0xfffffffc      /* For Base & Max Offset */
472
473 /* PCI Express capabilities */
474 #define PCI_EXP_DEVCAP          4       /* Device capabilities */
475 #define  PCI_EXP_DEVCAP_FLR     0x10000000 /* Function Level Reset */
476 #define PCI_EXP_DEVCTL          8       /* Device Control */
477 #define  PCI_EXP_DEVCTL_BCR_FLR 0x8000  /* Bridge Configuration Retry / FLR */
478
479 /* Include the ID list */
480
481 #include <pci_ids.h>
482
483 #ifndef __ASSEMBLY__
484
485 #ifdef CONFIG_SYS_PCI_64BIT
486 typedef u64 pci_addr_t;
487 typedef u64 pci_size_t;
488 #else
489 typedef u32 pci_addr_t;
490 typedef u32 pci_size_t;
491 #endif
492
493 struct pci_region {
494         pci_addr_t bus_start;   /* Start on the bus */
495         phys_addr_t phys_start; /* Start in physical address space */
496         pci_size_t size;        /* Size */
497         unsigned long flags;    /* Resource flags */
498
499         pci_addr_t bus_lower;
500 };
501
502 #define PCI_REGION_MEM          0x00000000      /* PCI memory space */
503 #define PCI_REGION_IO           0x00000001      /* PCI IO space */
504 #define PCI_REGION_TYPE         0x00000001
505 #define PCI_REGION_PREFETCH     0x00000008      /* prefetchable PCI memory */
506
507 #define PCI_REGION_SYS_MEMORY   0x00000100      /* System memory */
508 #define PCI_REGION_RO           0x00000200      /* Read-only memory */
509
510 static inline void pci_set_region(struct pci_region *reg,
511                                       pci_addr_t bus_start,
512                                       phys_addr_t phys_start,
513                                       pci_size_t size,
514                                       unsigned long flags) {
515         reg->bus_start  = bus_start;
516         reg->phys_start = phys_start;
517         reg->size       = size;
518         reg->flags      = flags;
519 }
520
521 typedef int pci_dev_t;
522
523 #define PCI_BUS(d)              (((d) >> 16) & 0xff)
524
525 /*
526  * Please note the difference in DEVFN usage in U-Boot vs Linux. U-Boot
527  * uses DEVFN in bits 15-8 but Linux instead expects DEVFN in bits 7-0.
528  * Please see the Linux header include/uapi/linux/pci.h for more details.
529  * This is relevant for the following macros:
530  * PCI_DEV, PCI_FUNC, PCI_DEVFN
531  * The U-Boot macro PCI_DEV is equivalent to the Linux PCI_SLOT version with
532  * the remark from above (input d in bits 15-8 instead of 7-0.
533  */
534 #define PCI_DEV(d)              (((d) >> 11) & 0x1f)
535 #define PCI_FUNC(d)             (((d) >> 8) & 0x7)
536 #define PCI_DEVFN(d, f)         ((d) << 11 | (f) << 8)
537
538 #define PCI_MASK_BUS(bdf)       ((bdf) & 0xffff)
539 #define PCI_ADD_BUS(bus, devfn) (((bus) << 16) | (devfn))
540 #define PCI_BDF(b, d, f)        ((b) << 16 | PCI_DEVFN(d, f))
541 #define PCI_VENDEV(v, d)        (((v) << 16) | (d))
542 #define PCI_ANY_ID              (~0)
543
544 struct pci_device_id {
545         unsigned int vendor, device;    /* Vendor and device ID or PCI_ANY_ID */
546         unsigned int subvendor, subdevice; /* Subsystem ID's or PCI_ANY_ID */
547         unsigned int class, class_mask; /* (class,subclass,prog-if) triplet */
548         unsigned long driver_data;      /* Data private to the driver */
549 };
550
551 struct pci_controller;
552
553 struct pci_config_table {
554         unsigned int vendor, device;            /* Vendor and device ID or PCI_ANY_ID */
555         unsigned int class;                     /* Class ID, or  PCI_ANY_ID */
556         unsigned int bus;                       /* Bus number, or PCI_ANY_ID */
557         unsigned int dev;                       /* Device number, or PCI_ANY_ID */
558         unsigned int func;                      /* Function number, or PCI_ANY_ID */
559
560         void (*config_device)(struct pci_controller* hose, pci_dev_t dev,
561                               struct pci_config_table *);
562         unsigned long priv[3];
563 };
564
565 extern void pci_cfgfunc_do_nothing(struct pci_controller* hose, pci_dev_t dev,
566                                    struct pci_config_table *);
567 extern void pci_cfgfunc_config_device(struct pci_controller* hose, pci_dev_t dev,
568                                       struct pci_config_table *);
569
570 #define MAX_PCI_REGIONS         7
571
572 #define INDIRECT_TYPE_NO_PCIE_LINK      1
573
574 /*
575  * Structure of a PCI controller (host bridge)
576  *
577  * With driver model this is dev_get_uclass_priv(bus)
578  */
579 struct pci_controller {
580 #ifdef CONFIG_DM_PCI
581         struct udevice *bus;
582         struct udevice *ctlr;
583 #else
584         struct pci_controller *next;
585 #endif
586
587         int first_busno;
588         int last_busno;
589
590         volatile unsigned int *cfg_addr;
591         volatile unsigned char *cfg_data;
592
593         int indirect_type;
594
595         /*
596          * TODO(sjg@chromium.org): With driver model we use struct
597          * pci_controller for both the controller and any bridge devices
598          * attached to it. But there is only one region list and it is in the
599          * top-level controller.
600          *
601          * This could be changed so that struct pci_controller is only used
602          * for PCI controllers and a separate UCLASS (or perhaps
603          * UCLASS_PCI_GENERIC) is used for bridges.
604          */
605         struct pci_region regions[MAX_PCI_REGIONS];
606         int region_count;
607
608         struct pci_config_table *config_table;
609
610         void (*fixup_irq)(struct pci_controller *, pci_dev_t);
611 #ifndef CONFIG_DM_PCI
612         /* Low-level architecture-dependent routines */
613         int (*read_byte)(struct pci_controller*, pci_dev_t, int where, u8 *);
614         int (*read_word)(struct pci_controller*, pci_dev_t, int where, u16 *);
615         int (*read_dword)(struct pci_controller*, pci_dev_t, int where, u32 *);
616         int (*write_byte)(struct pci_controller*, pci_dev_t, int where, u8);
617         int (*write_word)(struct pci_controller*, pci_dev_t, int where, u16);
618         int (*write_dword)(struct pci_controller*, pci_dev_t, int where, u32);
619 #endif
620
621         /* Used by auto config */
622         struct pci_region *pci_mem, *pci_io, *pci_prefetch;
623
624 #ifndef CONFIG_DM_PCI
625         int current_busno;
626
627         void *priv_data;
628 #endif
629 };
630
631 #ifndef CONFIG_DM_PCI
632 static inline void pci_set_ops(struct pci_controller *hose,
633                                    int (*read_byte)(struct pci_controller*,
634                                                     pci_dev_t, int where, u8 *),
635                                    int (*read_word)(struct pci_controller*,
636                                                     pci_dev_t, int where, u16 *),
637                                    int (*read_dword)(struct pci_controller*,
638                                                      pci_dev_t, int where, u32 *),
639                                    int (*write_byte)(struct pci_controller*,
640                                                      pci_dev_t, int where, u8),
641                                    int (*write_word)(struct pci_controller*,
642                                                      pci_dev_t, int where, u16),
643                                    int (*write_dword)(struct pci_controller*,
644                                                       pci_dev_t, int where, u32)) {
645         hose->read_byte   = read_byte;
646         hose->read_word   = read_word;
647         hose->read_dword  = read_dword;
648         hose->write_byte  = write_byte;
649         hose->write_word  = write_word;
650         hose->write_dword = write_dword;
651 }
652 #endif
653
654 #ifdef CONFIG_PCI_INDIRECT_BRIDGE
655 extern void pci_setup_indirect(struct pci_controller* hose, u32 cfg_addr, u32 cfg_data);
656 #endif
657
658 #if !defined(CONFIG_DM_PCI) || defined(CONFIG_DM_PCI_COMPAT)
659 extern phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose,
660                                         pci_addr_t addr, unsigned long flags);
661 extern pci_addr_t pci_hose_phys_to_bus(struct pci_controller* hose,
662                                         phys_addr_t addr, unsigned long flags);
663
664 #define pci_phys_to_bus(dev, addr, flags) \
665         pci_hose_phys_to_bus(pci_bus_to_hose(PCI_BUS(dev)), (addr), (flags))
666 #define pci_bus_to_phys(dev, addr, flags) \
667         pci_hose_bus_to_phys(pci_bus_to_hose(PCI_BUS(dev)), (addr), (flags))
668
669 #define pci_virt_to_bus(dev, addr, flags) \
670         pci_hose_phys_to_bus(pci_bus_to_hose(PCI_BUS(dev)), \
671                              (virt_to_phys(addr)), (flags))
672 #define pci_bus_to_virt(dev, addr, flags, len, map_flags) \
673         map_physmem(pci_hose_bus_to_phys(pci_bus_to_hose(PCI_BUS(dev)), \
674                                          (addr), (flags)), \
675                     (len), (map_flags))
676
677 #define pci_phys_to_mem(dev, addr) \
678         pci_phys_to_bus((dev), (addr), PCI_REGION_MEM)
679 #define pci_mem_to_phys(dev, addr) \
680         pci_bus_to_phys((dev), (addr), PCI_REGION_MEM)
681 #define pci_phys_to_io(dev, addr)  pci_phys_to_bus((dev), (addr), PCI_REGION_IO)
682 #define pci_io_to_phys(dev, addr)  pci_bus_to_phys((dev), (addr), PCI_REGION_IO)
683
684 #define pci_virt_to_mem(dev, addr) \
685         pci_virt_to_bus((dev), (addr), PCI_REGION_MEM)
686 #define pci_mem_to_virt(dev, addr, len, map_flags) \
687         pci_bus_to_virt((dev), (addr), PCI_REGION_MEM, (len), (map_flags))
688 #define pci_virt_to_io(dev, addr) \
689         pci_virt_to_bus((dev), (addr), PCI_REGION_IO)
690 #define pci_io_to_virt(dev, addr, len, map_flags) \
691         pci_bus_to_virt((dev), (addr), PCI_REGION_IO, (len), (map_flags))
692
693 /* For driver model these are defined in macros in pci_compat.c */
694 extern int pci_hose_read_config_byte(struct pci_controller *hose,
695                                      pci_dev_t dev, int where, u8 *val);
696 extern int pci_hose_read_config_word(struct pci_controller *hose,
697                                      pci_dev_t dev, int where, u16 *val);
698 extern int pci_hose_read_config_dword(struct pci_controller *hose,
699                                       pci_dev_t dev, int where, u32 *val);
700 extern int pci_hose_write_config_byte(struct pci_controller *hose,
701                                       pci_dev_t dev, int where, u8 val);
702 extern int pci_hose_write_config_word(struct pci_controller *hose,
703                                       pci_dev_t dev, int where, u16 val);
704 extern int pci_hose_write_config_dword(struct pci_controller *hose,
705                                        pci_dev_t dev, int where, u32 val);
706 #endif
707
708 #ifndef CONFIG_DM_PCI
709 extern int pci_read_config_byte(pci_dev_t dev, int where, u8 *val);
710 extern int pci_read_config_word(pci_dev_t dev, int where, u16 *val);
711 extern int pci_read_config_dword(pci_dev_t dev, int where, u32 *val);
712 extern int pci_write_config_byte(pci_dev_t dev, int where, u8 val);
713 extern int pci_write_config_word(pci_dev_t dev, int where, u16 val);
714 extern int pci_write_config_dword(pci_dev_t dev, int where, u32 val);
715 #endif
716
717 void pciauto_region_init(struct pci_region *res);
718 void pciauto_region_align(struct pci_region *res, pci_size_t size);
719 void pciauto_config_init(struct pci_controller *hose);
720
721 /**
722  * pciauto_region_allocate() - Allocate resources from a PCI resource region
723  *
724  * Allocates @size bytes from the PCI resource @res. If @supports_64bit is
725  * false, the result will be guaranteed to fit in 32 bits.
726  *
727  * @res:                PCI region to allocate from
728  * @size:               Amount of bytes to allocate
729  * @bar:                Returns the PCI bus address of the allocated resource
730  * @supports_64bit:     Whether to allow allocations above the 32-bit boundary
731  * @return 0 if successful, -1 on failure
732  */
733 int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
734                             pci_addr_t *bar, bool supports_64bit);
735
736 #if !defined(CONFIG_DM_PCI) || defined(CONFIG_DM_PCI_COMPAT)
737 extern int pci_hose_read_config_byte_via_dword(struct pci_controller *hose,
738                                                pci_dev_t dev, int where, u8 *val);
739 extern int pci_hose_read_config_word_via_dword(struct pci_controller *hose,
740                                                pci_dev_t dev, int where, u16 *val);
741 extern int pci_hose_write_config_byte_via_dword(struct pci_controller *hose,
742                                                 pci_dev_t dev, int where, u8 val);
743 extern int pci_hose_write_config_word_via_dword(struct pci_controller *hose,
744                                                 pci_dev_t dev, int where, u16 val);
745
746 extern void *pci_map_bar(pci_dev_t pdev, int bar, int flags);
747 extern void pci_register_hose(struct pci_controller* hose);
748 extern struct pci_controller* pci_bus_to_hose(int bus);
749 extern struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr);
750 extern struct pci_controller *pci_get_hose_head(void);
751
752 extern int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev);
753 extern int pci_hose_scan(struct pci_controller *hose);
754 extern int pci_hose_scan_bus(struct pci_controller *hose, int bus);
755
756 extern void pciauto_setup_device(struct pci_controller *hose,
757                                  pci_dev_t dev, int bars_num,
758                                  struct pci_region *mem,
759                                  struct pci_region *prefetch,
760                                  struct pci_region *io);
761 extern void pciauto_prescan_setup_bridge(struct pci_controller *hose,
762                                  pci_dev_t dev, int sub_bus);
763 extern void pciauto_postscan_setup_bridge(struct pci_controller *hose,
764                                  pci_dev_t dev, int sub_bus);
765 extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
766
767 extern pci_dev_t pci_find_device (unsigned int vendor, unsigned int device, int index);
768 extern pci_dev_t pci_find_devices (struct pci_device_id *ids, int index);
769 pci_dev_t pci_find_class(unsigned int find_class, int index);
770
771 extern int pci_hose_find_capability(struct pci_controller *hose, pci_dev_t dev,
772                                     int cap);
773 extern int pci_hose_find_cap_start(struct pci_controller *hose, pci_dev_t dev,
774                                    u8 hdr_type);
775 extern int pci_find_cap(struct pci_controller *hose, pci_dev_t dev, int pos,
776                         int cap);
777
778 int pci_find_next_ext_capability(struct pci_controller *hose,
779                                  pci_dev_t dev, int start, int cap);
780 int pci_hose_find_ext_capability(struct pci_controller *hose,
781                                  pci_dev_t dev, int cap);
782
783 #ifdef CONFIG_PCI_FIXUP_DEV
784 extern void board_pci_fixup_dev(struct pci_controller *hose, pci_dev_t dev,
785                                 unsigned short vendor,
786                                 unsigned short device,
787                                 unsigned short class);
788 #endif
789 #endif /* !defined(CONFIG_DM_PCI) || defined(CONFIG_DM_PCI_COMPAT) */
790
791 const char * pci_class_str(u8 class);
792 int pci_last_busno(void);
793
794 #ifdef CONFIG_MPC85xx
795 extern void pci_mpc85xx_init (struct pci_controller *hose);
796 #endif
797
798 #ifdef CONFIG_PCIE_IMX
799 extern void imx_pcie_remove(void);
800 #endif
801
802 #if !defined(CONFIG_DM_PCI) || defined(CONFIG_DM_PCI_COMPAT)
803 /**
804  * pci_write_bar32() - Write the address of a BAR including control bits
805  *
806  * This writes a raw address (with control bits) to a bar. This can be used
807  * with devices which require hard-coded addresses, not part of the normal
808  * PCI enumeration process.
809  *
810  * @hose:       PCI hose to use
811  * @dev:        PCI device to update
812  * @barnum:     BAR number (0-5)
813  * @addr:       BAR address with control bits
814  */
815 void pci_write_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum,
816                      u32 addr);
817
818 /**
819  * pci_read_bar32() - read the address of a bar
820  *
821  * @hose:       PCI hose to use
822  * @dev:        PCI device to inspect
823  * @barnum:     BAR number (0-5)
824  * @return address of the bar, masking out any control bits
825  * */
826 u32 pci_read_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum);
827
828 /**
829  * pci_hose_find_devices() - Find devices by vendor/device ID
830  *
831  * @hose:       PCI hose to search
832  * @busnum:     Bus number to search
833  * @ids:        PCI vendor/device IDs to look for, terminated by 0, 0 record
834  * @indexp:     Pointer to device index to find. To find the first matching
835  *              device, pass 0; to find the second, pass 1, etc. This
836  *              parameter is decremented for each non-matching device so
837  *              can be called repeatedly.
838  */
839 pci_dev_t pci_hose_find_devices(struct pci_controller *hose, int busnum,
840                                 struct pci_device_id *ids, int *indexp);
841 #endif /* !CONFIG_DM_PCI || CONFIG_DM_PCI_COMPAT */
842
843 /* Access sizes for PCI reads and writes */
844 enum pci_size_t {
845         PCI_SIZE_8,
846         PCI_SIZE_16,
847         PCI_SIZE_32,
848 };
849
850 struct udevice;
851
852 #ifdef CONFIG_DM_PCI
853 /**
854  * struct pci_child_platdata - information stored about each PCI device
855  *
856  * Every device on a PCI bus has this per-child data.
857  *
858  * It can be accessed using dev_get_parent_platdata(dev) if dev->parent is a
859  * PCI bus (i.e. UCLASS_PCI)
860  *
861  * @devfn:      Encoded device and function index - see PCI_DEVFN()
862  * @vendor:     PCI vendor ID (see pci_ids.h)
863  * @device:     PCI device ID (see pci_ids.h)
864  * @class:      PCI class, 3 bytes: (base, sub, prog-if)
865  */
866 struct pci_child_platdata {
867         int devfn;
868         unsigned short vendor;
869         unsigned short device;
870         unsigned int class;
871 };
872
873 /* PCI bus operations */
874 struct dm_pci_ops {
875         /**
876          * read_config() - Read a PCI configuration value
877          *
878          * PCI buses must support reading and writing configuration values
879          * so that the bus can be scanned and its devices configured.
880          *
881          * Normally PCI_BUS(@bdf) is the same as @bus->seq, but not always.
882          * If bridges exist it is possible to use the top-level bus to
883          * access a sub-bus. In that case @bus will be the top-level bus
884          * and PCI_BUS(bdf) will be a different (higher) value
885          *
886          * @bus:        Bus to read from
887          * @bdf:        Bus, device and function to read
888          * @offset:     Byte offset within the device's configuration space
889          * @valuep:     Place to put the returned value
890          * @size:       Access size
891          * @return 0 if OK, -ve on error
892          */
893         int (*read_config)(struct udevice *bus, pci_dev_t bdf, uint offset,
894                            ulong *valuep, enum pci_size_t size);
895         /**
896          * write_config() - Write a PCI configuration value
897          *
898          * @bus:        Bus to write to
899          * @bdf:        Bus, device and function to write
900          * @offset:     Byte offset within the device's configuration space
901          * @value:      Value to write
902          * @size:       Access size
903          * @return 0 if OK, -ve on error
904          */
905         int (*write_config)(struct udevice *bus, pci_dev_t bdf, uint offset,
906                             ulong value, enum pci_size_t size);
907 };
908
909 /* Get access to a PCI bus' operations */
910 #define pci_get_ops(dev)        ((struct dm_pci_ops *)(dev)->driver->ops)
911
912 /**
913  * dm_pci_get_bdf() - Get the BDF value for a device
914  *
915  * @dev:        Device to check
916  * @return bus/device/function value (see PCI_BDF())
917  */
918 pci_dev_t dm_pci_get_bdf(struct udevice *dev);
919
920 /**
921  * pci_bind_bus_devices() - scan a PCI bus and bind devices
922  *
923  * Scan a PCI bus looking for devices. Bind each one that is found. If
924  * devices are already bound that match the scanned devices, just update the
925  * child data so that the device can be used correctly (this happens when
926  * the device tree describes devices we expect to see on the bus).
927  *
928  * Devices that are bound in this way will use a generic PCI driver which
929  * does nothing. The device can still be accessed but will not provide any
930  * driver interface.
931  *
932  * @bus:        Bus containing devices to bind
933  * @return 0 if OK, -ve on error
934  */
935 int pci_bind_bus_devices(struct udevice *bus);
936
937 /**
938  * pci_auto_config_devices() - configure bus devices ready for use
939  *
940  * This works through all devices on a bus by scanning the driver model
941  * data structures (normally these have been set up by pci_bind_bus_devices()
942  * earlier).
943  *
944  * Space is allocated for each PCI base address register (BAR) so that the
945  * devices are mapped into memory and I/O space ready for use.
946  *
947  * @bus:        Bus containing devices to bind
948  * @return 0 if OK, -ve on error
949  */
950 int pci_auto_config_devices(struct udevice *bus);
951
952 /**
953  * dm_pci_bus_find_bdf() - Find a device given its PCI bus address
954  *
955  * @bdf:        PCI device address: bus, device and function -see PCI_BDF()
956  * @devp:       Returns the device for this address, if found
957  * @return 0 if OK, -ENODEV if not found
958  */
959 int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp);
960
961 /**
962  * pci_bus_find_devfn() - Find a device on a bus
963  *
964  * @find_devfn:         PCI device address (device and function only)
965  * @devp:       Returns the device for this address, if found
966  * @return 0 if OK, -ENODEV if not found
967  */
968 int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
969                        struct udevice **devp);
970
971 /**
972  * pci_find_first_device() - return the first available PCI device
973  *
974  * This function and pci_find_first_device() allow iteration through all
975  * available PCI devices on all buses. Assuming there are any, this will
976  * return the first one.
977  *
978  * @devp:       Set to the first available device, or NULL if no more are left
979  *              or we got an error
980  * @return 0 if all is OK, -ve on error (e.g. a bus/bridge failed to probe)
981  */
982 int pci_find_first_device(struct udevice **devp);
983
984 /**
985  * pci_find_next_device() - return the next available PCI device
986  *
987  * Finds the next available PCI device after the one supplied, or sets @devp
988  * to NULL if there are no more.
989  *
990  * @devp:       On entry, the last device returned. Set to the next available
991  *              device, or NULL if no more are left or we got an error
992  * @return 0 if all is OK, -ve on error (e.g. a bus/bridge failed to probe)
993  */
994 int pci_find_next_device(struct udevice **devp);
995
996 /**
997  * pci_get_ff() - Returns a mask for the given access size
998  *
999  * @size:       Access size
1000  * @return 0xff for PCI_SIZE_8, 0xffff for PCI_SIZE_16, 0xffffffff for
1001  * PCI_SIZE_32
1002  */
1003 int pci_get_ff(enum pci_size_t size);
1004
1005 /**
1006  * pci_bus_find_devices () - Find devices on a bus
1007  *
1008  * @bus:        Bus to search
1009  * @ids:        PCI vendor/device IDs to look for, terminated by 0, 0 record
1010  * @indexp:     Pointer to device index to find. To find the first matching
1011  *              device, pass 0; to find the second, pass 1, etc. This
1012  *              parameter is decremented for each non-matching device so
1013  *              can be called repeatedly.
1014  * @devp:       Returns matching device if found
1015  * @return 0 if found, -ENODEV if not
1016  */
1017 int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
1018                          int *indexp, struct udevice **devp);
1019
1020 /**
1021  * pci_find_device_id() - Find a device on any bus
1022  *
1023  * @ids:        PCI vendor/device IDs to look for, terminated by 0, 0 record
1024  * @index:      Index number of device to find, 0 for the first match, 1 for
1025  *              the second, etc.
1026  * @devp:       Returns matching device if found
1027  * @return 0 if found, -ENODEV if not
1028  */
1029 int pci_find_device_id(struct pci_device_id *ids, int index,
1030                        struct udevice **devp);
1031
1032 /**
1033  * dm_pci_hose_probe_bus() - probe a subordinate bus, scanning it for devices
1034  *
1035  * This probes the given bus which causes it to be scanned for devices. The
1036  * devices will be bound but not probed.
1037  *
1038  * @hose specifies the PCI hose that will be used for the scan. This is
1039  * always a top-level bus with uclass UCLASS_PCI. The bus to scan is
1040  * in @bdf, and is a subordinate bus reachable from @hose.
1041  *
1042  * @hose:       PCI hose to scan
1043  * @bdf:        PCI bus address to scan (PCI_BUS(bdf) is the bus number)
1044  * @return 0 if OK, -ve on error
1045  */
1046 int dm_pci_hose_probe_bus(struct udevice *bus);
1047
1048 /**
1049  * pci_bus_read_config() - Read a configuration value from a device
1050  *
1051  * TODO(sjg@chromium.org): We should be able to pass just a device and have
1052  * it do the right thing. It would be good to have that function also.
1053  *
1054  * @bus:        Bus to read from
1055  * @bdf:        PCI device address: bus, device and function -see PCI_BDF()
1056  * @offset:     Register offset to read
1057  * @valuep:     Place to put the returned value
1058  * @size:       Access size
1059  * @return 0 if OK, -ve on error
1060  */
1061 int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
1062                         unsigned long *valuep, enum pci_size_t size);
1063
1064 /**
1065  * pci_bus_write_config() - Write a configuration value to a device
1066  *
1067  * @bus:        Bus to write from
1068  * @bdf:        PCI device address: bus, device and function -see PCI_BDF()
1069  * @offset:     Register offset to write
1070  * @value:      Value to write
1071  * @size:       Access size
1072  * @return 0 if OK, -ve on error
1073  */
1074 int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
1075                          unsigned long value, enum pci_size_t size);
1076
1077 /**
1078  * pci_bus_clrset_config32() - Update a configuration value for a device
1079  *
1080  * The register at @offset is updated to (oldvalue & ~clr) | set.
1081  *
1082  * @bus:        Bus to access
1083  * @bdf:        PCI device address: bus, device and function -see PCI_BDF()
1084  * @offset:     Register offset to update
1085  * @clr:        Bits to clear
1086  * @set:        Bits to set
1087  * @return 0 if OK, -ve on error
1088  */
1089 int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
1090                             u32 clr, u32 set);
1091
1092 /**
1093  * Driver model PCI config access functions. Use these in preference to others
1094  * when you have a valid device
1095  */
1096 int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
1097                        enum pci_size_t size);
1098
1099 int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep);
1100 int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep);
1101 int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep);
1102
1103 int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
1104                         enum pci_size_t size);
1105
1106 int dm_pci_write_config8(struct udevice *dev, int offset, u8 value);
1107 int dm_pci_write_config16(struct udevice *dev, int offset, u16 value);
1108 int dm_pci_write_config32(struct udevice *dev, int offset, u32 value);
1109
1110 /**
1111  * These permit convenient read/modify/write on PCI configuration. The
1112  * register is updated to (oldvalue & ~clr) | set.
1113  */
1114 int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set);
1115 int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set);
1116 int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set);
1117
1118 /*
1119  * The following functions provide access to the above without needing the
1120  * size parameter. We are trying to encourage the use of the 8/16/32-style
1121  * functions, rather than byte/word/dword. But both are supported.
1122  */
1123 int pci_write_config32(pci_dev_t pcidev, int offset, u32 value);
1124 int pci_write_config16(pci_dev_t pcidev, int offset, u16 value);
1125 int pci_write_config8(pci_dev_t pcidev, int offset, u8 value);
1126 int pci_read_config32(pci_dev_t pcidev, int offset, u32 *valuep);
1127 int pci_read_config16(pci_dev_t pcidev, int offset, u16 *valuep);
1128 int pci_read_config8(pci_dev_t pcidev, int offset, u8 *valuep);
1129
1130 /**
1131  * pci_generic_mmap_write_config() - Generic helper for writing to
1132  * memory-mapped PCI configuration space.
1133  * @bus: Pointer to the PCI bus
1134  * @addr_f: Callback for calculating the config space address
1135  * @bdf: Identifies the PCI device to access
1136  * @offset: The offset into the device's configuration space
1137  * @value: The value to write
1138  * @size: Indicates the size of access to perform
1139  *
1140  * Write the value @value of size @size from offset @offset within the
1141  * configuration space of the device identified by the bus, device & function
1142  * numbers in @bdf on the PCI bus @bus. The callback function @addr_f is
1143  * responsible for calculating the CPU address of the respective configuration
1144  * space offset.
1145  *
1146  * Return: 0 on success, else -EINVAL
1147  */
1148 int pci_generic_mmap_write_config(
1149         struct udevice *bus,
1150         int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
1151         pci_dev_t bdf,
1152         uint offset,
1153         ulong value,
1154         enum pci_size_t size);
1155
1156 /**
1157  * pci_generic_mmap_read_config() - Generic helper for reading from
1158  * memory-mapped PCI configuration space.
1159  * @bus: Pointer to the PCI bus
1160  * @addr_f: Callback for calculating the config space address
1161  * @bdf: Identifies the PCI device to access
1162  * @offset: The offset into the device's configuration space
1163  * @valuep: A pointer at which to store the read value
1164  * @size: Indicates the size of access to perform
1165  *
1166  * Read a value of size @size from offset @offset within the configuration
1167  * space of the device identified by the bus, device & function numbers in @bdf
1168  * on the PCI bus @bus. The callback function @addr_f is responsible for
1169  * calculating the CPU address of the respective configuration space offset.
1170  *
1171  * Return: 0 on success, else -EINVAL
1172  */
1173 int pci_generic_mmap_read_config(
1174         struct udevice *bus,
1175         int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
1176         pci_dev_t bdf,
1177         uint offset,
1178         ulong *valuep,
1179         enum pci_size_t size);
1180
1181 #ifdef CONFIG_DM_PCI_COMPAT
1182 /* Compatibility with old naming */
1183 static inline int pci_write_config_dword(pci_dev_t pcidev, int offset,
1184                                          u32 value)
1185 {
1186         return pci_write_config32(pcidev, offset, value);
1187 }
1188
1189 /* Compatibility with old naming */
1190 static inline int pci_write_config_word(pci_dev_t pcidev, int offset,
1191                                         u16 value)
1192 {
1193         return pci_write_config16(pcidev, offset, value);
1194 }
1195
1196 /* Compatibility with old naming */
1197 static inline int pci_write_config_byte(pci_dev_t pcidev, int offset,
1198                                         u8 value)
1199 {
1200         return pci_write_config8(pcidev, offset, value);
1201 }
1202
1203 /* Compatibility with old naming */
1204 static inline int pci_read_config_dword(pci_dev_t pcidev, int offset,
1205                                         u32 *valuep)
1206 {
1207         return pci_read_config32(pcidev, offset, valuep);
1208 }
1209
1210 /* Compatibility with old naming */
1211 static inline int pci_read_config_word(pci_dev_t pcidev, int offset,
1212                                        u16 *valuep)
1213 {
1214         return pci_read_config16(pcidev, offset, valuep);
1215 }
1216
1217 /* Compatibility with old naming */
1218 static inline int pci_read_config_byte(pci_dev_t pcidev, int offset,
1219                                        u8 *valuep)
1220 {
1221         return pci_read_config8(pcidev, offset, valuep);
1222 }
1223 #endif /* CONFIG_DM_PCI_COMPAT */
1224
1225 /**
1226  * dm_pciauto_config_device() - configure a device ready for use
1227  *
1228  * Space is allocated for each PCI base address register (BAR) so that the
1229  * devices are mapped into memory and I/O space ready for use.
1230  *
1231  * @dev:        Device to configure
1232  * @return 0 if OK, -ve on error
1233  */
1234 int dm_pciauto_config_device(struct udevice *dev);
1235
1236 /**
1237  * pci_conv_32_to_size() - convert a 32-bit read value to the given size
1238  *
1239  * Some PCI buses must always perform 32-bit reads. The data must then be
1240  * shifted and masked to reflect the required access size and offset. This
1241  * function performs this transformation.
1242  *
1243  * @value:      Value to transform (32-bit value read from @offset & ~3)
1244  * @offset:     Register offset that was read
1245  * @size:       Required size of the result
1246  * @return the value that would have been obtained if the read had been
1247  * performed at the given offset with the correct size
1248  */
1249 ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size);
1250
1251 /**
1252  * pci_conv_size_to_32() - update a 32-bit value to prepare for a write
1253  *
1254  * Some PCI buses must always perform 32-bit writes. To emulate a smaller
1255  * write the old 32-bit data must be read, updated with the required new data
1256  * and written back as a 32-bit value. This function performs the
1257  * transformation from the old value to the new value.
1258  *
1259  * @value:      Value to transform (32-bit value read from @offset & ~3)
1260  * @offset:     Register offset that should be written
1261  * @size:       Required size of the write
1262  * @return the value that should be written as a 32-bit access to @offset & ~3.
1263  */
1264 ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1265                           enum pci_size_t size);
1266
1267 /**
1268  * pci_get_controller() - obtain the controller to use for a bus
1269  *
1270  * @dev:        Device to check
1271  * @return pointer to the controller device for this bus
1272  */
1273 struct udevice *pci_get_controller(struct udevice *dev);
1274
1275 /**
1276  * pci_get_regions() - obtain pointers to all the region types
1277  *
1278  * @dev:        Device to check
1279  * @iop:        Returns a pointer to the I/O region, or NULL if none
1280  * @memp:       Returns a pointer to the memory region, or NULL if none
1281  * @prefp:      Returns a pointer to the pre-fetch region, or NULL if none
1282  * @return the number of non-NULL regions returned, normally 3
1283  */
1284 int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1285                     struct pci_region **memp, struct pci_region **prefp);
1286
1287 /**
1288  * dm_pci_write_bar32() - Write the address of a BAR
1289  *
1290  * This writes a raw address to a bar
1291  *
1292  * @dev:        PCI device to update
1293  * @barnum:     BAR number (0-5)
1294  * @addr:       BAR address
1295  */
1296 void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr);
1297
1298 /**
1299  * dm_pci_read_bar32() - read a base address register from a device
1300  *
1301  * @dev:        Device to check
1302  * @barnum:     Bar number to read (numbered from 0)
1303  * @return: value of BAR
1304  */
1305 u32 dm_pci_read_bar32(struct udevice *dev, int barnum);
1306
1307 /**
1308  * dm_pci_bus_to_phys() - convert a PCI bus address to a physical address
1309  *
1310  * @dev:        Device containing the PCI address
1311  * @addr:       PCI address to convert
1312  * @flags:      Flags for the region type (PCI_REGION_...)
1313  * @return physical address corresponding to that PCI bus address
1314  */
1315 phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t addr,
1316                                unsigned long flags);
1317
1318 /**
1319  * dm_pci_phys_to_bus() - convert a physical address to a PCI bus address
1320  *
1321  * @dev:        Device containing the bus address
1322  * @addr:       Physical address to convert
1323  * @flags:      Flags for the region type (PCI_REGION_...)
1324  * @return PCI bus address corresponding to that physical address
1325  */
1326 pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t addr,
1327                               unsigned long flags);
1328
1329 /**
1330  * dm_pci_map_bar() - get a virtual address associated with a BAR region
1331  *
1332  * Looks up a base address register and finds the physical memory address
1333  * that corresponds to it.
1334  * Can be used for 32b BARs 0-5 on type 0 functions and for 32b BARs 0-1 on
1335  * type 1 functions.
1336  * Can also be used on type 0 functions that support Enhanced Allocation for
1337  * 32b/64b BARs.  Note that duplicate BEI entries are not supported.
1338  *
1339  * @dev:        Device to check
1340  * @bar:        Bar register offset (PCI_BASE_ADDRESS_...)
1341  * @flags:      Flags for the region type (PCI_REGION_...)
1342  * @return: pointer to the virtual address to use or 0 on error
1343  */
1344 void *dm_pci_map_bar(struct udevice *dev, int bar, int flags);
1345
1346 /**
1347  * dm_pci_find_next_capability() - find a capability starting from an offset
1348  *
1349  * Tell if a device supports a given PCI capability. Returns the
1350  * address of the requested capability structure within the device's
1351  * PCI configuration space or 0 in case the device does not support it.
1352  *
1353  * Possible values for @cap:
1354  *
1355  *  %PCI_CAP_ID_MSI     Message Signalled Interrupts
1356  *  %PCI_CAP_ID_PCIX    PCI-X
1357  *  %PCI_CAP_ID_EXP     PCI Express
1358  *  %PCI_CAP_ID_MSIX    MSI-X
1359  *
1360  * See PCI_CAP_ID_xxx for the complete capability ID codes.
1361  *
1362  * @dev:        PCI device to query
1363  * @start:      offset to start from
1364  * @cap:        capability code
1365  * @return:     capability address or 0 if not supported
1366  */
1367 int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap);
1368
1369 /**
1370  * dm_pci_find_capability() - find a capability
1371  *
1372  * Tell if a device supports a given PCI capability. Returns the
1373  * address of the requested capability structure within the device's
1374  * PCI configuration space or 0 in case the device does not support it.
1375  *
1376  * Possible values for @cap:
1377  *
1378  *  %PCI_CAP_ID_MSI     Message Signalled Interrupts
1379  *  %PCI_CAP_ID_PCIX    PCI-X
1380  *  %PCI_CAP_ID_EXP     PCI Express
1381  *  %PCI_CAP_ID_MSIX    MSI-X
1382  *
1383  * See PCI_CAP_ID_xxx for the complete capability ID codes.
1384  *
1385  * @dev:        PCI device to query
1386  * @cap:        capability code
1387  * @return:     capability address or 0 if not supported
1388  */
1389 int dm_pci_find_capability(struct udevice *dev, int cap);
1390
1391 /**
1392  * dm_pci_find_next_ext_capability() - find an extended capability
1393  *                                     starting from an offset
1394  *
1395  * Tell if a device supports a given PCI express extended capability.
1396  * Returns the address of the requested extended capability structure
1397  * within the device's PCI configuration space or 0 in case the device
1398  * does not support it.
1399  *
1400  * Possible values for @cap:
1401  *
1402  *  %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
1403  *  %PCI_EXT_CAP_ID_VC  Virtual Channel
1404  *  %PCI_EXT_CAP_ID_DSN Device Serial Number
1405  *  %PCI_EXT_CAP_ID_PWR Power Budgeting
1406  *
1407  * See PCI_EXT_CAP_ID_xxx for the complete extended capability ID codes.
1408  *
1409  * @dev:        PCI device to query
1410  * @start:      offset to start from
1411  * @cap:        extended capability code
1412  * @return:     extended capability address or 0 if not supported
1413  */
1414 int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap);
1415
1416 /**
1417  * dm_pci_find_ext_capability() - find an extended capability
1418  *
1419  * Tell if a device supports a given PCI express extended capability.
1420  * Returns the address of the requested extended capability structure
1421  * within the device's PCI configuration space or 0 in case the device
1422  * does not support it.
1423  *
1424  * Possible values for @cap:
1425  *
1426  *  %PCI_EXT_CAP_ID_ERR Advanced Error Reporting
1427  *  %PCI_EXT_CAP_ID_VC  Virtual Channel
1428  *  %PCI_EXT_CAP_ID_DSN Device Serial Number
1429  *  %PCI_EXT_CAP_ID_PWR Power Budgeting
1430  *
1431  * See PCI_EXT_CAP_ID_xxx for the complete extended capability ID codes.
1432  *
1433  * @dev:        PCI device to query
1434  * @cap:        extended capability code
1435  * @return:     extended capability address or 0 if not supported
1436  */
1437 int dm_pci_find_ext_capability(struct udevice *dev, int cap);
1438
1439 /**
1440  * dm_pci_flr() - Perform FLR if the device suppoorts it
1441  *
1442  * @dev:        PCI device to reset
1443  * @return:     0 if OK, -ENOENT if FLR is not supported by dev
1444  */
1445 int dm_pci_flr(struct udevice *dev);
1446
1447 #define dm_pci_virt_to_bus(dev, addr, flags) \
1448         dm_pci_phys_to_bus(dev, (virt_to_phys(addr)), (flags))
1449 #define dm_pci_bus_to_virt(dev, addr, flags, len, map_flags) \
1450         map_physmem(dm_pci_bus_to_phys(dev, (addr), (flags)), \
1451                     (len), (map_flags))
1452
1453 #define dm_pci_phys_to_mem(dev, addr) \
1454         dm_pci_phys_to_bus((dev), (addr), PCI_REGION_MEM)
1455 #define dm_pci_mem_to_phys(dev, addr) \
1456         dm_pci_bus_to_phys((dev), (addr), PCI_REGION_MEM)
1457 #define dm_pci_phys_to_io(dev, addr) \
1458         dm_pci_phys_to_bus((dev), (addr), PCI_REGION_IO)
1459 #define dm_pci_io_to_phys(dev, addr) \
1460         dm_pci_bus_to_phys((dev), (addr), PCI_REGION_IO)
1461
1462 #define dm_pci_virt_to_mem(dev, addr) \
1463         dm_pci_virt_to_bus((dev), (addr), PCI_REGION_MEM)
1464 #define dm_pci_mem_to_virt(dev, addr, len, map_flags) \
1465         dm_pci_bus_to_virt((dev), (addr), PCI_REGION_MEM, (len), (map_flags))
1466 #define dm_pci_virt_to_io(dev, addr) \
1467         dm_pci_virt_to_bus((dev), (addr), PCI_REGION_IO)
1468 #define dm_pci_io_to_virt(dev, addr, len, map_flags) \
1469         dm_pci_bus_to_virt((dev), (addr), PCI_REGION_IO, (len), (map_flags))
1470
1471 /**
1472  * dm_pci_find_device() - find a device by vendor/device ID
1473  *
1474  * @vendor:     Vendor ID
1475  * @device:     Device ID
1476  * @index:      0 to find the first match, 1 for second, etc.
1477  * @devp:       Returns pointer to the device, if found
1478  * @return 0 if found, -ve on error
1479  */
1480 int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
1481                        struct udevice **devp);
1482
1483 /**
1484  * dm_pci_find_class() - find a device by class
1485  *
1486  * @find_class: 3-byte (24-bit) class value to find
1487  * @index:      0 to find the first match, 1 for second, etc.
1488  * @devp:       Returns pointer to the device, if found
1489  * @return 0 if found, -ve on error
1490  */
1491 int dm_pci_find_class(uint find_class, int index, struct udevice **devp);
1492
1493 /**
1494  * struct pci_emul_uc_priv - holds info about an emulator device
1495  *
1496  * There is always at most one emulator per client
1497  *
1498  * @client: Client device if any, else NULL
1499  */
1500 struct pci_emul_uc_priv {
1501         struct udevice *client;
1502 };
1503
1504 /**
1505  * struct dm_pci_emul_ops - PCI device emulator operations
1506  */
1507 struct dm_pci_emul_ops {
1508         /**
1509          * read_config() - Read a PCI configuration value
1510          *
1511          * @dev:        Emulated device to read from
1512          * @offset:     Byte offset within the device's configuration space
1513          * @valuep:     Place to put the returned value
1514          * @size:       Access size
1515          * @return 0 if OK, -ve on error
1516          */
1517         int (*read_config)(struct udevice *dev, uint offset, ulong *valuep,
1518                            enum pci_size_t size);
1519         /**
1520          * write_config() - Write a PCI configuration value
1521          *
1522          * @dev:        Emulated device to write to
1523          * @offset:     Byte offset within the device's configuration space
1524          * @value:      Value to write
1525          * @size:       Access size
1526          * @return 0 if OK, -ve on error
1527          */
1528         int (*write_config)(struct udevice *dev, uint offset, ulong value,
1529                             enum pci_size_t size);
1530         /**
1531          * read_io() - Read a PCI I/O value
1532          *
1533          * @dev:        Emulated device to read from
1534          * @addr:       I/O address to read
1535          * @valuep:     Place to put the returned value
1536          * @size:       Access size
1537          * @return 0 if OK, -ENOENT if @addr is not mapped by this device,
1538          *              other -ve value on error
1539          */
1540         int (*read_io)(struct udevice *dev, unsigned int addr, ulong *valuep,
1541                        enum pci_size_t size);
1542         /**
1543          * write_io() - Write a PCI I/O value
1544          *
1545          * @dev:        Emulated device to write from
1546          * @addr:       I/O address to write
1547          * @value:      Value to write
1548          * @size:       Access size
1549          * @return 0 if OK, -ENOENT if @addr is not mapped by this device,
1550          *              other -ve value on error
1551          */
1552         int (*write_io)(struct udevice *dev, unsigned int addr,
1553                         ulong value, enum pci_size_t size);
1554         /**
1555          * map_physmem() - Map a device into sandbox memory
1556          *
1557          * @dev:        Emulated device to map
1558          * @addr:       Memory address, normally corresponding to a PCI BAR.
1559          *              The device should have been configured to have a BAR
1560          *              at this address.
1561          * @lenp:       On entry, the size of the area to map, On exit it is
1562          *              updated to the size actually mapped, which may be less
1563          *              if the device has less space
1564          * @ptrp:       Returns a pointer to the mapped address. The device's
1565          *              space can be accessed as @lenp bytes starting here
1566          * @return 0 if OK, -ENOENT if @addr is not mapped by this device,
1567          *              other -ve value on error
1568          */
1569         int (*map_physmem)(struct udevice *dev, phys_addr_t addr,
1570                            unsigned long *lenp, void **ptrp);
1571         /**
1572          * unmap_physmem() - undo a memory mapping
1573          *
1574          * This must be called after map_physmem() to undo the mapping.
1575          * Some devices can use this to check what has been written into
1576          * their mapped memory and perform an operations they require on it.
1577          * In this way, map/unmap can be used as a sort of handshake between
1578          * the emulated device and its users.
1579          *
1580          * @dev:        Emuated device to unmap
1581          * @vaddr:      Mapped memory address, as passed to map_physmem()
1582          * @len:        Size of area mapped, as returned by map_physmem()
1583          * @return 0 if OK, -ve on error
1584          */
1585         int (*unmap_physmem)(struct udevice *dev, const void *vaddr,
1586                              unsigned long len);
1587 };
1588
1589 /* Get access to a PCI device emulator's operations */
1590 #define pci_get_emul_ops(dev)   ((struct dm_pci_emul_ops *)(dev)->driver->ops)
1591
1592 /**
1593  * sandbox_pci_get_emul() - Get the emulation device for a PCI device
1594  *
1595  * Searches for a suitable emulator for the given PCI bus device
1596  *
1597  * @bus:        PCI bus to search
1598  * @find_devfn: PCI device and function address (PCI_DEVFN())
1599  * @containerp: Returns container device if found
1600  * @emulp:      Returns emulated device if found
1601  * @return 0 if found, -ENODEV if not found
1602  */
1603 int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
1604                          struct udevice **containerp, struct udevice **emulp);
1605
1606 /**
1607  * sandbox_pci_get_client() - Find the client for an emulation device
1608  *
1609  * @emul:       Emulation device to check
1610  * @devp:       Returns the client device emulated by this device
1611  * @return 0 if OK, -ENOENT if the device has no client yet
1612  */
1613 int sandbox_pci_get_client(struct udevice *emul, struct udevice **devp);
1614
1615 /**
1616  * pci_get_devfn() - Extract the devfn from fdt_pci_addr of the device
1617  *
1618  * Get devfn from fdt_pci_addr of the specified device
1619  *
1620  * @dev:        PCI device
1621  * @return devfn in bits 15...8 if found, -ENODEV if not found
1622  */
1623 int pci_get_devfn(struct udevice *dev);
1624
1625 #endif /* CONFIG_DM_PCI */
1626
1627 /**
1628  * PCI_DEVICE - macro used to describe a specific pci device
1629  * @vend: the 16 bit PCI Vendor ID
1630  * @dev: the 16 bit PCI Device ID
1631  *
1632  * This macro is used to create a struct pci_device_id that matches a
1633  * specific device.  The subvendor and subdevice fields will be set to
1634  * PCI_ANY_ID.
1635  */
1636 #define PCI_DEVICE(vend, dev) \
1637         .vendor = (vend), .device = (dev), \
1638         .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
1639
1640 /**
1641  * PCI_DEVICE_SUB - macro used to describe a specific pci device with subsystem
1642  * @vend: the 16 bit PCI Vendor ID
1643  * @dev: the 16 bit PCI Device ID
1644  * @subvend: the 16 bit PCI Subvendor ID
1645  * @subdev: the 16 bit PCI Subdevice ID
1646  *
1647  * This macro is used to create a struct pci_device_id that matches a
1648  * specific device with subsystem information.
1649  */
1650 #define PCI_DEVICE_SUB(vend, dev, subvend, subdev) \
1651         .vendor = (vend), .device = (dev), \
1652         .subvendor = (subvend), .subdevice = (subdev)
1653
1654 /**
1655  * PCI_DEVICE_CLASS - macro used to describe a specific pci device class
1656  * @dev_class: the class, subclass, prog-if triple for this device
1657  * @dev_class_mask: the class mask for this device
1658  *
1659  * This macro is used to create a struct pci_device_id that matches a
1660  * specific PCI class.  The vendor, device, subvendor, and subdevice
1661  * fields will be set to PCI_ANY_ID.
1662  */
1663 #define PCI_DEVICE_CLASS(dev_class, dev_class_mask) \
1664         .class = (dev_class), .class_mask = (dev_class_mask), \
1665         .vendor = PCI_ANY_ID, .device = PCI_ANY_ID, \
1666         .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
1667
1668 /**
1669  * PCI_VDEVICE - macro used to describe a specific pci device in short form
1670  * @vend: the vendor name
1671  * @dev: the 16 bit PCI Device ID
1672  *
1673  * This macro is used to create a struct pci_device_id that matches a
1674  * specific PCI device.  The subvendor, and subdevice fields will be set
1675  * to PCI_ANY_ID. The macro allows the next field to follow as the device
1676  * private data.
1677  */
1678
1679 #define PCI_VDEVICE(vend, dev) \
1680         .vendor = PCI_VENDOR_ID_##vend, .device = (dev), \
1681         .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, 0, 0
1682
1683 /**
1684  * struct pci_driver_entry - Matches a driver to its pci_device_id list
1685  * @driver: Driver to use
1686  * @match: List of match records for this driver, terminated by {}
1687  */
1688 struct pci_driver_entry {
1689         struct driver *driver;
1690         const struct pci_device_id *match;
1691 };
1692
1693 #define U_BOOT_PCI_DEVICE(__name, __match)                              \
1694         ll_entry_declare(struct pci_driver_entry, __name, pci_driver_entry) = {\
1695                 .driver = llsym(struct driver, __name, driver), \
1696                 .match = __match, \
1697                 }
1698
1699 #endif /* __ASSEMBLY__ */
1700 #endif /* _PCI_H */