blob: 83a40a5adf25a79d20593e7a6228064c21f3e4ad [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenkc6097192002-11-03 00:24:07 +00002/*
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.
wdenkc6097192002-11-03 00:24:07 +00008 */
9
10#ifndef _PCI_H
11#define _PCI_H
12
Minghuan Lianed5b5802015-07-10 11:35:08 +080013#define PCI_CFG_SPACE_SIZE 256
14#define PCI_CFG_SPACE_EXP_SIZE 4096
15
wdenkc6097192002-11-03 00:24:07 +000016/*
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_VENDOR_ID 0x00 /* 16 bits */
21#define PCI_DEVICE_ID 0x02 /* 16 bits */
22#define PCI_COMMAND 0x04 /* 16 bits */
23#define PCI_COMMAND_IO 0x1 /* Enable response in I/O space */
24#define PCI_COMMAND_MEMORY 0x2 /* Enable response in Memory space */
25#define PCI_COMMAND_MASTER 0x4 /* Enable bus mastering */
26#define PCI_COMMAND_SPECIAL 0x8 /* Enable response to special cycles */
27#define PCI_COMMAND_INVALIDATE 0x10 /* Use memory write and invalidate */
28#define PCI_COMMAND_VGA_PALETTE 0x20 /* Enable palette snooping */
29#define PCI_COMMAND_PARITY 0x40 /* Enable parity checking */
30#define PCI_COMMAND_WAIT 0x80 /* Enable address/data stepping */
31#define PCI_COMMAND_SERR 0x100 /* Enable SERR */
32#define PCI_COMMAND_FAST_BACK 0x200 /* Enable back-to-back writes */
33
34#define PCI_STATUS 0x06 /* 16 bits */
35#define PCI_STATUS_CAP_LIST 0x10 /* Support Capability List */
36#define PCI_STATUS_66MHZ 0x20 /* Support 66 Mhz PCI 2.1 bus */
37#define PCI_STATUS_UDF 0x40 /* Support User Definable Features [obsolete] */
38#define PCI_STATUS_FAST_BACK 0x80 /* Accept fast-back to back */
39#define PCI_STATUS_PARITY 0x100 /* Detected parity error */
40#define PCI_STATUS_DEVSEL_MASK 0x600 /* DEVSEL timing */
41#define PCI_STATUS_DEVSEL_FAST 0x000
42#define PCI_STATUS_DEVSEL_MEDIUM 0x200
43#define PCI_STATUS_DEVSEL_SLOW 0x400
44#define PCI_STATUS_SIG_TARGET_ABORT 0x800 /* Set on target abort */
45#define PCI_STATUS_REC_TARGET_ABORT 0x1000 /* Master ack of " */
46#define PCI_STATUS_REC_MASTER_ABORT 0x2000 /* Set on master abort */
47#define PCI_STATUS_SIG_SYSTEM_ERROR 0x4000 /* Set when we drive SERR */
48#define PCI_STATUS_DETECTED_PARITY 0x8000 /* Set on parity error */
49
50#define PCI_CLASS_REVISION 0x08 /* High 24 bits are class, low 8
51 revision */
52#define PCI_REVISION_ID 0x08 /* Revision ID */
53#define PCI_CLASS_PROG 0x09 /* Reg. Level Programming Interface */
54#define PCI_CLASS_DEVICE 0x0a /* Device class */
55#define PCI_CLASS_CODE 0x0b /* Device class code */
Bill Richardson55ae10f2012-10-20 11:44:34 +000056#define PCI_CLASS_CODE_TOO_OLD 0x00
57#define PCI_CLASS_CODE_STORAGE 0x01
58#define PCI_CLASS_CODE_NETWORK 0x02
59#define PCI_CLASS_CODE_DISPLAY 0x03
60#define PCI_CLASS_CODE_MULTIMEDIA 0x04
61#define PCI_CLASS_CODE_MEMORY 0x05
62#define PCI_CLASS_CODE_BRIDGE 0x06
63#define PCI_CLASS_CODE_COMM 0x07
64#define PCI_CLASS_CODE_PERIPHERAL 0x08
65#define PCI_CLASS_CODE_INPUT 0x09
66#define PCI_CLASS_CODE_DOCKING 0x0A
67#define PCI_CLASS_CODE_PROCESSOR 0x0B
68#define PCI_CLASS_CODE_SERIAL 0x0C
69#define PCI_CLASS_CODE_WIRELESS 0x0D
70#define PCI_CLASS_CODE_I2O 0x0E
71#define PCI_CLASS_CODE_SATELLITE 0x0F
72#define PCI_CLASS_CODE_CRYPTO 0x10
73#define PCI_CLASS_CODE_DATA 0x11
74/* Base Class 0x12 - 0xFE is reserved */
75#define PCI_CLASS_CODE_OTHER 0xFF
76
wdenkc6097192002-11-03 00:24:07 +000077#define PCI_CLASS_SUB_CODE 0x0a /* Device sub-class code */
Bill Richardson55ae10f2012-10-20 11:44:34 +000078#define PCI_CLASS_SUB_CODE_TOO_OLD_NOTVGA 0x00
79#define PCI_CLASS_SUB_CODE_TOO_OLD_VGA 0x01
80#define PCI_CLASS_SUB_CODE_STORAGE_SCSI 0x00
81#define PCI_CLASS_SUB_CODE_STORAGE_IDE 0x01
82#define PCI_CLASS_SUB_CODE_STORAGE_FLOPPY 0x02
83#define PCI_CLASS_SUB_CODE_STORAGE_IPIBUS 0x03
84#define PCI_CLASS_SUB_CODE_STORAGE_RAID 0x04
85#define PCI_CLASS_SUB_CODE_STORAGE_ATA 0x05
86#define PCI_CLASS_SUB_CODE_STORAGE_SATA 0x06
87#define PCI_CLASS_SUB_CODE_STORAGE_SAS 0x07
88#define PCI_CLASS_SUB_CODE_STORAGE_OTHER 0x80
89#define PCI_CLASS_SUB_CODE_NETWORK_ETHERNET 0x00
90#define PCI_CLASS_SUB_CODE_NETWORK_TOKENRING 0x01
91#define PCI_CLASS_SUB_CODE_NETWORK_FDDI 0x02
92#define PCI_CLASS_SUB_CODE_NETWORK_ATM 0x03
93#define PCI_CLASS_SUB_CODE_NETWORK_ISDN 0x04
94#define PCI_CLASS_SUB_CODE_NETWORK_WORLDFIP 0x05
95#define PCI_CLASS_SUB_CODE_NETWORK_PICMG 0x06
96#define PCI_CLASS_SUB_CODE_NETWORK_OTHER 0x80
97#define PCI_CLASS_SUB_CODE_DISPLAY_VGA 0x00
98#define PCI_CLASS_SUB_CODE_DISPLAY_XGA 0x01
99#define PCI_CLASS_SUB_CODE_DISPLAY_3D 0x02
100#define PCI_CLASS_SUB_CODE_DISPLAY_OTHER 0x80
101#define PCI_CLASS_SUB_CODE_MULTIMEDIA_VIDEO 0x00
102#define PCI_CLASS_SUB_CODE_MULTIMEDIA_AUDIO 0x01
103#define PCI_CLASS_SUB_CODE_MULTIMEDIA_PHONE 0x02
104#define PCI_CLASS_SUB_CODE_MULTIMEDIA_OTHER 0x80
105#define PCI_CLASS_SUB_CODE_MEMORY_RAM 0x00
106#define PCI_CLASS_SUB_CODE_MEMORY_FLASH 0x01
107#define PCI_CLASS_SUB_CODE_MEMORY_OTHER 0x80
108#define PCI_CLASS_SUB_CODE_BRIDGE_HOST 0x00
109#define PCI_CLASS_SUB_CODE_BRIDGE_ISA 0x01
110#define PCI_CLASS_SUB_CODE_BRIDGE_EISA 0x02
111#define PCI_CLASS_SUB_CODE_BRIDGE_MCA 0x03
112#define PCI_CLASS_SUB_CODE_BRIDGE_PCI 0x04
113#define PCI_CLASS_SUB_CODE_BRIDGE_PCMCIA 0x05
114#define PCI_CLASS_SUB_CODE_BRIDGE_NUBUS 0x06
115#define PCI_CLASS_SUB_CODE_BRIDGE_CARDBUS 0x07
116#define PCI_CLASS_SUB_CODE_BRIDGE_RACEWAY 0x08
117#define PCI_CLASS_SUB_CODE_BRIDGE_SEMI_PCI 0x09
118#define PCI_CLASS_SUB_CODE_BRIDGE_INFINIBAND 0x0A
119#define PCI_CLASS_SUB_CODE_BRIDGE_OTHER 0x80
120#define PCI_CLASS_SUB_CODE_COMM_SERIAL 0x00
121#define PCI_CLASS_SUB_CODE_COMM_PARALLEL 0x01
122#define PCI_CLASS_SUB_CODE_COMM_MULTIPORT 0x02
123#define PCI_CLASS_SUB_CODE_COMM_MODEM 0x03
124#define PCI_CLASS_SUB_CODE_COMM_GPIB 0x04
125#define PCI_CLASS_SUB_CODE_COMM_SMARTCARD 0x05
126#define PCI_CLASS_SUB_CODE_COMM_OTHER 0x80
127#define PCI_CLASS_SUB_CODE_PERIPHERAL_PIC 0x00
128#define PCI_CLASS_SUB_CODE_PERIPHERAL_DMA 0x01
129#define PCI_CLASS_SUB_CODE_PERIPHERAL_TIMER 0x02
130#define PCI_CLASS_SUB_CODE_PERIPHERAL_RTC 0x03
131#define PCI_CLASS_SUB_CODE_PERIPHERAL_HOTPLUG 0x04
132#define PCI_CLASS_SUB_CODE_PERIPHERAL_SD 0x05
133#define PCI_CLASS_SUB_CODE_PERIPHERAL_OTHER 0x80
134#define PCI_CLASS_SUB_CODE_INPUT_KEYBOARD 0x00
135#define PCI_CLASS_SUB_CODE_INPUT_DIGITIZER 0x01
136#define PCI_CLASS_SUB_CODE_INPUT_MOUSE 0x02
137#define PCI_CLASS_SUB_CODE_INPUT_SCANNER 0x03
138#define PCI_CLASS_SUB_CODE_INPUT_GAMEPORT 0x04
139#define PCI_CLASS_SUB_CODE_INPUT_OTHER 0x80
140#define PCI_CLASS_SUB_CODE_DOCKING_GENERIC 0x00
141#define PCI_CLASS_SUB_CODE_DOCKING_OTHER 0x80
142#define PCI_CLASS_SUB_CODE_PROCESSOR_386 0x00
143#define PCI_CLASS_SUB_CODE_PROCESSOR_486 0x01
144#define PCI_CLASS_SUB_CODE_PROCESSOR_PENTIUM 0x02
145#define PCI_CLASS_SUB_CODE_PROCESSOR_ALPHA 0x10
146#define PCI_CLASS_SUB_CODE_PROCESSOR_POWERPC 0x20
147#define PCI_CLASS_SUB_CODE_PROCESSOR_MIPS 0x30
148#define PCI_CLASS_SUB_CODE_PROCESSOR_COPROC 0x40
149#define PCI_CLASS_SUB_CODE_SERIAL_1394 0x00
150#define PCI_CLASS_SUB_CODE_SERIAL_ACCESSBUS 0x01
151#define PCI_CLASS_SUB_CODE_SERIAL_SSA 0x02
152#define PCI_CLASS_SUB_CODE_SERIAL_USB 0x03
153#define PCI_CLASS_SUB_CODE_SERIAL_FIBRECHAN 0x04
154#define PCI_CLASS_SUB_CODE_SERIAL_SMBUS 0x05
155#define PCI_CLASS_SUB_CODE_SERIAL_INFINIBAND 0x06
156#define PCI_CLASS_SUB_CODE_SERIAL_IPMI 0x07
157#define PCI_CLASS_SUB_CODE_SERIAL_SERCOS 0x08
158#define PCI_CLASS_SUB_CODE_SERIAL_CANBUS 0x09
159#define PCI_CLASS_SUB_CODE_WIRELESS_IRDA 0x00
160#define PCI_CLASS_SUB_CODE_WIRELESS_IR 0x01
161#define PCI_CLASS_SUB_CODE_WIRELESS_RF 0x10
162#define PCI_CLASS_SUB_CODE_WIRELESS_BLUETOOTH 0x11
163#define PCI_CLASS_SUB_CODE_WIRELESS_BROADBAND 0x12
164#define PCI_CLASS_SUB_CODE_WIRELESS_80211A 0x20
165#define PCI_CLASS_SUB_CODE_WIRELESS_80211B 0x21
166#define PCI_CLASS_SUB_CODE_WIRELESS_OTHER 0x80
167#define PCI_CLASS_SUB_CODE_I2O_V1_0 0x00
168#define PCI_CLASS_SUB_CODE_SATELLITE_TV 0x01
169#define PCI_CLASS_SUB_CODE_SATELLITE_AUDIO 0x02
170#define PCI_CLASS_SUB_CODE_SATELLITE_VOICE 0x03
171#define PCI_CLASS_SUB_CODE_SATELLITE_DATA 0x04
172#define PCI_CLASS_SUB_CODE_CRYPTO_NETWORK 0x00
173#define PCI_CLASS_SUB_CODE_CRYPTO_ENTERTAINMENT 0x10
174#define PCI_CLASS_SUB_CODE_CRYPTO_OTHER 0x80
175#define PCI_CLASS_SUB_CODE_DATA_DPIO 0x00
176#define PCI_CLASS_SUB_CODE_DATA_PERFCNTR 0x01
177#define PCI_CLASS_SUB_CODE_DATA_COMMSYNC 0x10
178#define PCI_CLASS_SUB_CODE_DATA_MGMT 0x20
179#define PCI_CLASS_SUB_CODE_DATA_OTHER 0x80
wdenkc6097192002-11-03 00:24:07 +0000180
181#define PCI_CACHE_LINE_SIZE 0x0c /* 8 bits */
182#define PCI_LATENCY_TIMER 0x0d /* 8 bits */
183#define PCI_HEADER_TYPE 0x0e /* 8 bits */
184#define PCI_HEADER_TYPE_NORMAL 0
185#define PCI_HEADER_TYPE_BRIDGE 1
186#define PCI_HEADER_TYPE_CARDBUS 2
187
188#define PCI_BIST 0x0f /* 8 bits */
189#define PCI_BIST_CODE_MASK 0x0f /* Return result */
190#define PCI_BIST_START 0x40 /* 1 to start BIST, 2 secs or less */
191#define PCI_BIST_CAPABLE 0x80 /* 1 if BIST capable */
192
193/*
194 * Base addresses specify locations in memory or I/O space.
195 * Decoded size can be determined by writing a value of
196 * 0xffffffff to the register, and reading it back. Only
197 * 1 bits are decoded.
198 */
199#define PCI_BASE_ADDRESS_0 0x10 /* 32 bits */
200#define PCI_BASE_ADDRESS_1 0x14 /* 32 bits [htype 0,1 only] */
201#define PCI_BASE_ADDRESS_2 0x18 /* 32 bits [htype 0 only] */
202#define PCI_BASE_ADDRESS_3 0x1c /* 32 bits */
203#define PCI_BASE_ADDRESS_4 0x20 /* 32 bits */
204#define PCI_BASE_ADDRESS_5 0x24 /* 32 bits */
205#define PCI_BASE_ADDRESS_SPACE 0x01 /* 0 = memory, 1 = I/O */
206#define PCI_BASE_ADDRESS_SPACE_IO 0x01
207#define PCI_BASE_ADDRESS_SPACE_MEMORY 0x00
208#define PCI_BASE_ADDRESS_MEM_TYPE_MASK 0x06
209#define PCI_BASE_ADDRESS_MEM_TYPE_32 0x00 /* 32 bit address */
210#define PCI_BASE_ADDRESS_MEM_TYPE_1M 0x02 /* Below 1M [obsolete] */
211#define PCI_BASE_ADDRESS_MEM_TYPE_64 0x04 /* 64 bit address */
212#define PCI_BASE_ADDRESS_MEM_PREFETCH 0x08 /* prefetchable? */
Kumar Gala30e76d52008-10-21 08:36:08 -0500213#define PCI_BASE_ADDRESS_MEM_MASK (~0x0fULL)
214#define PCI_BASE_ADDRESS_IO_MASK (~0x03ULL)
wdenkc6097192002-11-03 00:24:07 +0000215/* bit 1 is reserved if address_space = 1 */
216
217/* Header type 0 (normal devices) */
218#define PCI_CARDBUS_CIS 0x28
219#define PCI_SUBSYSTEM_VENDOR_ID 0x2c
220#define PCI_SUBSYSTEM_ID 0x2e
221#define PCI_ROM_ADDRESS 0x30 /* Bits 31..11 are address, 10..1 reserved */
222#define PCI_ROM_ADDRESS_ENABLE 0x01
Kumar Gala30e76d52008-10-21 08:36:08 -0500223#define PCI_ROM_ADDRESS_MASK (~0x7ffULL)
wdenkc6097192002-11-03 00:24:07 +0000224
225#define PCI_CAPABILITY_LIST 0x34 /* Offset of first capability list entry */
226
227/* 0x35-0x3b are reserved */
228#define PCI_INTERRUPT_LINE 0x3c /* 8 bits */
229#define PCI_INTERRUPT_PIN 0x3d /* 8 bits */
230#define PCI_MIN_GNT 0x3e /* 8 bits */
231#define PCI_MAX_LAT 0x3f /* 8 bits */
232
Simon Glass5f48d792015-07-27 15:47:17 -0600233#define PCI_INTERRUPT_LINE_DISABLE 0xff
234
wdenkc6097192002-11-03 00:24:07 +0000235/* Header type 1 (PCI-to-PCI bridges) */
236#define PCI_PRIMARY_BUS 0x18 /* Primary bus number */
237#define PCI_SECONDARY_BUS 0x19 /* Secondary bus number */
238#define PCI_SUBORDINATE_BUS 0x1a /* Highest bus number behind the bridge */
239#define PCI_SEC_LATENCY_TIMER 0x1b /* Latency timer for secondary interface */
240#define PCI_IO_BASE 0x1c /* I/O range behind the bridge */
241#define PCI_IO_LIMIT 0x1d
242#define PCI_IO_RANGE_TYPE_MASK 0x0f /* I/O bridging type */
243#define PCI_IO_RANGE_TYPE_16 0x00
244#define PCI_IO_RANGE_TYPE_32 0x01
245#define PCI_IO_RANGE_MASK ~0x0f
246#define PCI_SEC_STATUS 0x1e /* Secondary status register, only bit 14 used */
247#define PCI_MEMORY_BASE 0x20 /* Memory range behind */
248#define PCI_MEMORY_LIMIT 0x22
249#define PCI_MEMORY_RANGE_TYPE_MASK 0x0f
250#define PCI_MEMORY_RANGE_MASK ~0x0f
251#define PCI_PREF_MEMORY_BASE 0x24 /* Prefetchable memory range behind */
252#define PCI_PREF_MEMORY_LIMIT 0x26
253#define PCI_PREF_RANGE_TYPE_MASK 0x0f
254#define PCI_PREF_RANGE_TYPE_32 0x00
255#define PCI_PREF_RANGE_TYPE_64 0x01
256#define PCI_PREF_RANGE_MASK ~0x0f
257#define PCI_PREF_BASE_UPPER32 0x28 /* Upper half of prefetchable memory range */
258#define PCI_PREF_LIMIT_UPPER32 0x2c
259#define PCI_IO_BASE_UPPER16 0x30 /* Upper half of I/O addresses */
260#define PCI_IO_LIMIT_UPPER16 0x32
261/* 0x34 same as for htype 0 */
262/* 0x35-0x3b is reserved */
263#define PCI_ROM_ADDRESS1 0x38 /* Same as PCI_ROM_ADDRESS, but for htype 1 */
264/* 0x3c-0x3d are same as for htype 0 */
265#define PCI_BRIDGE_CONTROL 0x3e
266#define PCI_BRIDGE_CTL_PARITY 0x01 /* Enable parity detection on secondary interface */
267#define PCI_BRIDGE_CTL_SERR 0x02 /* The same for SERR forwarding */
268#define PCI_BRIDGE_CTL_NO_ISA 0x04 /* Disable bridging of ISA ports */
269#define PCI_BRIDGE_CTL_VGA 0x08 /* Forward VGA addresses */
270#define PCI_BRIDGE_CTL_MASTER_ABORT 0x20 /* Report master aborts */
271#define PCI_BRIDGE_CTL_BUS_RESET 0x40 /* Secondary bus reset */
272#define PCI_BRIDGE_CTL_FAST_BACK 0x80 /* Fast Back2Back enabled on secondary interface */
273
274/* Header type 2 (CardBus bridges) */
275#define PCI_CB_CAPABILITY_LIST 0x14
276/* 0x15 reserved */
277#define PCI_CB_SEC_STATUS 0x16 /* Secondary status */
278#define PCI_CB_PRIMARY_BUS 0x18 /* PCI bus number */
279#define PCI_CB_CARD_BUS 0x19 /* CardBus bus number */
280#define PCI_CB_SUBORDINATE_BUS 0x1a /* Subordinate bus number */
281#define PCI_CB_LATENCY_TIMER 0x1b /* CardBus latency timer */
282#define PCI_CB_MEMORY_BASE_0 0x1c
283#define PCI_CB_MEMORY_LIMIT_0 0x20
284#define PCI_CB_MEMORY_BASE_1 0x24
285#define PCI_CB_MEMORY_LIMIT_1 0x28
286#define PCI_CB_IO_BASE_0 0x2c
287#define PCI_CB_IO_BASE_0_HI 0x2e
288#define PCI_CB_IO_LIMIT_0 0x30
289#define PCI_CB_IO_LIMIT_0_HI 0x32
290#define PCI_CB_IO_BASE_1 0x34
291#define PCI_CB_IO_BASE_1_HI 0x36
292#define PCI_CB_IO_LIMIT_1 0x38
293#define PCI_CB_IO_LIMIT_1_HI 0x3a
294#define PCI_CB_IO_RANGE_MASK ~0x03
295/* 0x3c-0x3d are same as for htype 0 */
296#define PCI_CB_BRIDGE_CONTROL 0x3e
297#define PCI_CB_BRIDGE_CTL_PARITY 0x01 /* Similar to standard bridge control register */
298#define PCI_CB_BRIDGE_CTL_SERR 0x02
299#define PCI_CB_BRIDGE_CTL_ISA 0x04
300#define PCI_CB_BRIDGE_CTL_VGA 0x08
301#define PCI_CB_BRIDGE_CTL_MASTER_ABORT 0x20
302#define PCI_CB_BRIDGE_CTL_CB_RESET 0x40 /* CardBus reset */
303#define PCI_CB_BRIDGE_CTL_16BIT_INT 0x80 /* Enable interrupt for 16-bit cards */
304#define PCI_CB_BRIDGE_CTL_PREFETCH_MEM0 0x100 /* Prefetch enable for both memory regions */
305#define PCI_CB_BRIDGE_CTL_PREFETCH_MEM1 0x200
306#define PCI_CB_BRIDGE_CTL_POST_WRITES 0x400
307#define PCI_CB_SUBSYSTEM_VENDOR_ID 0x40
308#define PCI_CB_SUBSYSTEM_ID 0x42
309#define PCI_CB_LEGACY_MODE_BASE 0x44 /* 16-bit PC Card legacy mode base address (ExCa) */
310/* 0x48-0x7f reserved */
311
312/* Capability lists */
313
314#define PCI_CAP_LIST_ID 0 /* Capability ID */
315#define PCI_CAP_ID_PM 0x01 /* Power Management */
316#define PCI_CAP_ID_AGP 0x02 /* Accelerated Graphics Port */
317#define PCI_CAP_ID_VPD 0x03 /* Vital Product Data */
318#define PCI_CAP_ID_SLOTID 0x04 /* Slot Identification */
319#define PCI_CAP_ID_MSI 0x05 /* Message Signalled Interrupts */
320#define PCI_CAP_ID_CHSWP 0x06 /* CompactPCI HotSwap */
Bin Meng5d544f92018-08-03 01:14:51 -0700321#define PCI_CAP_ID_PCIX 0x07 /* PCI-X */
322#define PCI_CAP_ID_HT 0x08 /* HyperTransport */
323#define PCI_CAP_ID_VNDR 0x09 /* Vendor-Specific */
324#define PCI_CAP_ID_DBG 0x0A /* Debug port */
325#define PCI_CAP_ID_CCRC 0x0B /* CompactPCI Central Resource Control */
326#define PCI_CAP_ID_SHPC 0x0C /* PCI Standard Hot-Plug Controller */
327#define PCI_CAP_ID_SSVID 0x0D /* Bridge subsystem vendor/device ID */
328#define PCI_CAP_ID_AGP3 0x0E /* AGP Target PCI-PCI bridge */
329#define PCI_CAP_ID_SECDEV 0x0F /* Secure Device */
330#define PCI_CAP_ID_EXP 0x10 /* PCI Express */
331#define PCI_CAP_ID_MSIX 0x11 /* MSI-X */
332#define PCI_CAP_ID_SATA 0x12 /* SATA Data/Index Conf. */
333#define PCI_CAP_ID_AF 0x13 /* PCI Advanced Features */
334#define PCI_CAP_ID_EA 0x14 /* PCI Enhanced Allocation */
335#define PCI_CAP_ID_MAX PCI_CAP_ID_EA
wdenkc6097192002-11-03 00:24:07 +0000336#define PCI_CAP_LIST_NEXT 1 /* Next capability in the list */
337#define PCI_CAP_FLAGS 2 /* Capability defined flags (16 bits) */
338#define PCI_CAP_SIZEOF 4
339
340/* Power Management Registers */
341
342#define PCI_PM_CAP_VER_MASK 0x0007 /* Version */
343#define PCI_PM_CAP_PME_CLOCK 0x0008 /* PME clock required */
344#define PCI_PM_CAP_AUX_POWER 0x0010 /* Auxilliary power support */
345#define PCI_PM_CAP_DSI 0x0020 /* Device specific initialization */
346#define PCI_PM_CAP_D1 0x0200 /* D1 power state support */
347#define PCI_PM_CAP_D2 0x0400 /* D2 power state support */
348#define PCI_PM_CAP_PME 0x0800 /* PME pin supported */
349#define PCI_PM_CTRL 4 /* PM control and status register */
350#define PCI_PM_CTRL_STATE_MASK 0x0003 /* Current power state (D0 to D3) */
351#define PCI_PM_CTRL_PME_ENABLE 0x0100 /* PME pin enable */
352#define PCI_PM_CTRL_DATA_SEL_MASK 0x1e00 /* Data select (??) */
353#define PCI_PM_CTRL_DATA_SCALE_MASK 0x6000 /* Data scale (??) */
354#define PCI_PM_CTRL_PME_STATUS 0x8000 /* PME pin status */
355#define PCI_PM_PPB_EXTENSIONS 6 /* PPB support extensions (??) */
356#define PCI_PM_PPB_B2_B3 0x40 /* Stop clock when in D3hot (??) */
357#define PCI_PM_BPCC_ENABLE 0x80 /* Bus power/clock control enable (??) */
358#define PCI_PM_DATA_REGISTER 7 /* (??) */
359#define PCI_PM_SIZEOF 8
360
361/* AGP registers */
362
363#define PCI_AGP_VERSION 2 /* BCD version number */
364#define PCI_AGP_RFU 3 /* Rest of capability flags */
365#define PCI_AGP_STATUS 4 /* Status register */
366#define PCI_AGP_STATUS_RQ_MASK 0xff000000 /* Maximum number of requests - 1 */
367#define PCI_AGP_STATUS_SBA 0x0200 /* Sideband addressing supported */
368#define PCI_AGP_STATUS_64BIT 0x0020 /* 64-bit addressing supported */
369#define PCI_AGP_STATUS_FW 0x0010 /* FW transfers supported */
370#define PCI_AGP_STATUS_RATE4 0x0004 /* 4x transfer rate supported */
371#define PCI_AGP_STATUS_RATE2 0x0002 /* 2x transfer rate supported */
372#define PCI_AGP_STATUS_RATE1 0x0001 /* 1x transfer rate supported */
373#define PCI_AGP_COMMAND 8 /* Control register */
374#define PCI_AGP_COMMAND_RQ_MASK 0xff000000 /* Master: Maximum number of requests */
375#define PCI_AGP_COMMAND_SBA 0x0200 /* Sideband addressing enabled */
376#define PCI_AGP_COMMAND_AGP 0x0100 /* Allow processing of AGP transactions */
377#define PCI_AGP_COMMAND_64BIT 0x0020 /* Allow processing of 64-bit addresses */
378#define PCI_AGP_COMMAND_FW 0x0010 /* Force FW transfers */
379#define PCI_AGP_COMMAND_RATE4 0x0004 /* Use 4x rate */
380#define PCI_AGP_COMMAND_RATE2 0x0002 /* Use 4x rate */
381#define PCI_AGP_COMMAND_RATE1 0x0001 /* Use 4x rate */
382#define PCI_AGP_SIZEOF 12
383
Matthew McClintockf0e6f572006-06-28 10:44:49 -0500384/* PCI-X registers */
385
386#define PCI_X_CMD_DPERR_E 0x0001 /* Data Parity Error Recovery Enable */
387#define PCI_X_CMD_ERO 0x0002 /* Enable Relaxed Ordering */
388#define PCI_X_CMD_MAX_READ 0x0000 /* Max Memory Read Byte Count */
389#define PCI_X_CMD_MAX_SPLIT 0x0030 /* Max Outstanding Split Transactions */
390#define PCI_X_CMD_VERSION(x) (((x) >> 12) & 3) /* Version */
391
392
wdenkc6097192002-11-03 00:24:07 +0000393/* Slot Identification */
394
395#define PCI_SID_ESR 2 /* Expansion Slot Register */
396#define PCI_SID_ESR_NSLOTS 0x1f /* Number of expansion slots available */
397#define PCI_SID_ESR_FIC 0x20 /* First In Chassis Flag */
398#define PCI_SID_CHASSIS_NR 3 /* Chassis Number */
399
400/* Message Signalled Interrupts registers */
401
402#define PCI_MSI_FLAGS 2 /* Various flags */
403#define PCI_MSI_FLAGS_64BIT 0x80 /* 64-bit addresses allowed */
404#define PCI_MSI_FLAGS_QSIZE 0x70 /* Message queue size configured */
405#define PCI_MSI_FLAGS_QMASK 0x0e /* Maximum queue size available */
406#define PCI_MSI_FLAGS_ENABLE 0x01 /* MSI feature enabled */
407#define PCI_MSI_RFU 3 /* Rest of capability flags */
408#define PCI_MSI_ADDRESS_LO 4 /* Lower 32 bits */
409#define PCI_MSI_ADDRESS_HI 8 /* Upper 32 bits (if PCI_MSI_FLAGS_64BIT set) */
410#define PCI_MSI_DATA_32 8 /* 16 bits of data for 32-bit devices */
411#define PCI_MSI_DATA_64 12 /* 16 bits of data for 64-bit devices */
412
413#define PCI_MAX_PCI_DEVICES 32
414#define PCI_MAX_PCI_FUNCTIONS 8
415
Zhao Qiang287df012013-10-12 13:46:33 +0800416#define PCI_FIND_CAP_TTL 0x48
417#define CAP_START_POS 0x40
418
Minghuan Lianed5b5802015-07-10 11:35:08 +0800419/* Extended Capabilities (PCI-X 2.0 and Express) */
420#define PCI_EXT_CAP_ID(header) (header & 0x0000ffff)
421#define PCI_EXT_CAP_VER(header) ((header >> 16) & 0xf)
422#define PCI_EXT_CAP_NEXT(header) ((header >> 20) & 0xffc)
423
424#define PCI_EXT_CAP_ID_ERR 0x01 /* Advanced Error Reporting */
425#define PCI_EXT_CAP_ID_VC 0x02 /* Virtual Channel Capability */
426#define PCI_EXT_CAP_ID_DSN 0x03 /* Device Serial Number */
427#define PCI_EXT_CAP_ID_PWR 0x04 /* Power Budgeting */
428#define PCI_EXT_CAP_ID_RCLD 0x05 /* Root Complex Link Declaration */
429#define PCI_EXT_CAP_ID_RCILC 0x06 /* Root Complex Internal Link Control */
430#define PCI_EXT_CAP_ID_RCEC 0x07 /* Root Complex Event Collector */
431#define PCI_EXT_CAP_ID_MFVC 0x08 /* Multi-Function VC Capability */
432#define PCI_EXT_CAP_ID_VC9 0x09 /* same as _VC */
433#define PCI_EXT_CAP_ID_RCRB 0x0A /* Root Complex RB? */
434#define PCI_EXT_CAP_ID_VNDR 0x0B /* Vendor-Specific */
435#define PCI_EXT_CAP_ID_CAC 0x0C /* Config Access - obsolete */
436#define PCI_EXT_CAP_ID_ACS 0x0D /* Access Control Services */
437#define PCI_EXT_CAP_ID_ARI 0x0E /* Alternate Routing ID */
438#define PCI_EXT_CAP_ID_ATS 0x0F /* Address Translation Services */
439#define PCI_EXT_CAP_ID_SRIOV 0x10 /* Single Root I/O Virtualization */
440#define PCI_EXT_CAP_ID_MRIOV 0x11 /* Multi Root I/O Virtualization */
441#define PCI_EXT_CAP_ID_MCAST 0x12 /* Multicast */
442#define PCI_EXT_CAP_ID_PRI 0x13 /* Page Request Interface */
443#define PCI_EXT_CAP_ID_AMD_XXX 0x14 /* Reserved for AMD */
444#define PCI_EXT_CAP_ID_REBAR 0x15 /* Resizable BAR */
445#define PCI_EXT_CAP_ID_DPA 0x16 /* Dynamic Power Allocation */
446#define PCI_EXT_CAP_ID_TPH 0x17 /* TPH Requester */
447#define PCI_EXT_CAP_ID_LTR 0x18 /* Latency Tolerance Reporting */
448#define PCI_EXT_CAP_ID_SECPCI 0x19 /* Secondary PCIe Capability */
449#define PCI_EXT_CAP_ID_PMUX 0x1A /* Protocol Multiplexing */
450#define PCI_EXT_CAP_ID_PASID 0x1B /* Process Address Space ID */
Bin Meng5d544f92018-08-03 01:14:51 -0700451#define PCI_EXT_CAP_ID_DPC 0x1D /* Downstream Port Containment */
452#define PCI_EXT_CAP_ID_L1SS 0x1E /* L1 PM Substates */
453#define PCI_EXT_CAP_ID_PTM 0x1F /* Precision Time Measurement */
454#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_PTM
Minghuan Lianed5b5802015-07-10 11:35:08 +0800455
wdenkc6097192002-11-03 00:24:07 +0000456/* Include the ID list */
457
458#include <pci_ids.h>
459
Paul Burtonfa5cec02013-11-08 11:18:47 +0000460#ifndef __ASSEMBLY__
461
Kumar Gala30e76d52008-10-21 08:36:08 -0500462#ifdef CONFIG_SYS_PCI_64BIT
463typedef u64 pci_addr_t;
464typedef u64 pci_size_t;
465#else
466typedef u32 pci_addr_t;
467typedef u32 pci_size_t;
468#endif
wdenkc6097192002-11-03 00:24:07 +0000469
Kumar Gala30e76d52008-10-21 08:36:08 -0500470struct pci_region {
471 pci_addr_t bus_start; /* Start on the bus */
472 phys_addr_t phys_start; /* Start in physical address space */
473 pci_size_t size; /* Size */
474 unsigned long flags; /* Resource flags */
475
476 pci_addr_t bus_lower;
wdenkc6097192002-11-03 00:24:07 +0000477};
478
479#define PCI_REGION_MEM 0x00000000 /* PCI memory space */
480#define PCI_REGION_IO 0x00000001 /* PCI IO space */
481#define PCI_REGION_TYPE 0x00000001
Kumar Galaa1790122006-01-11 13:24:15 -0600482#define PCI_REGION_PREFETCH 0x00000008 /* prefetchable PCI memory */
wdenkc6097192002-11-03 00:24:07 +0000483
Kumar Galaff4e66e2009-02-06 09:49:31 -0600484#define PCI_REGION_SYS_MEMORY 0x00000100 /* System memory */
wdenkc6097192002-11-03 00:24:07 +0000485#define PCI_REGION_RO 0x00000200 /* Read-only memory */
486
Simon Glassbc3442a2013-06-11 11:14:33 -0700487static inline void pci_set_region(struct pci_region *reg,
Kumar Gala30e76d52008-10-21 08:36:08 -0500488 pci_addr_t bus_start,
Becky Bruce36f32672008-05-07 13:24:57 -0500489 phys_addr_t phys_start,
Kumar Gala30e76d52008-10-21 08:36:08 -0500490 pci_size_t size,
wdenkc6097192002-11-03 00:24:07 +0000491 unsigned long flags) {
492 reg->bus_start = bus_start;
493 reg->phys_start = phys_start;
494 reg->size = size;
495 reg->flags = flags;
496}
497
498typedef int pci_dev_t;
499
Simon Glassff3e0772015-03-05 12:25:25 -0700500#define PCI_BUS(d) (((d) >> 16) & 0xff)
501#define PCI_DEV(d) (((d) >> 11) & 0x1f)
502#define PCI_FUNC(d) (((d) >> 8) & 0x7)
503#define PCI_DEVFN(d, f) ((d) << 11 | (f) << 8)
504#define PCI_MASK_BUS(bdf) ((bdf) & 0xffff)
505#define PCI_ADD_BUS(bus, devfn) (((bus) << 16) | (devfn))
506#define PCI_BDF(b, d, f) ((b) << 16 | PCI_DEVFN(d, f))
507#define PCI_VENDEV(v, d) (((v) << 16) | (d))
508#define PCI_ANY_ID (~0)
wdenkc6097192002-11-03 00:24:07 +0000509
510struct pci_device_id {
Simon Glassaba92962015-07-06 16:47:44 -0600511 unsigned int vendor, device; /* Vendor and device ID or PCI_ANY_ID */
512 unsigned int subvendor, subdevice; /* Subsystem ID's or PCI_ANY_ID */
513 unsigned int class, class_mask; /* (class,subclass,prog-if) triplet */
514 unsigned long driver_data; /* Data private to the driver */
wdenkc6097192002-11-03 00:24:07 +0000515};
516
517struct pci_controller;
518
519struct pci_config_table {
520 unsigned int vendor, device; /* Vendor and device ID or PCI_ANY_ID */
521 unsigned int class; /* Class ID, or PCI_ANY_ID */
522 unsigned int bus; /* Bus number, or PCI_ANY_ID */
523 unsigned int dev; /* Device number, or PCI_ANY_ID */
524 unsigned int func; /* Function number, or PCI_ANY_ID */
525
526 void (*config_device)(struct pci_controller* hose, pci_dev_t dev,
527 struct pci_config_table *);
528 unsigned long priv[3];
529};
530
Wolfgang Denk993a2272006-03-12 16:54:11 +0100531extern void pci_cfgfunc_do_nothing(struct pci_controller* hose, pci_dev_t dev,
532 struct pci_config_table *);
wdenkc6097192002-11-03 00:24:07 +0000533extern void pci_cfgfunc_config_device(struct pci_controller* hose, pci_dev_t dev,
534 struct pci_config_table *);
535
536#define MAX_PCI_REGIONS 7
537
Anton Vorontsovfd6646c2009-01-08 04:26:12 +0300538#define INDIRECT_TYPE_NO_PCIE_LINK 1
539
wdenkc6097192002-11-03 00:24:07 +0000540/*
541 * Structure of a PCI controller (host bridge)
Simon Glass54fe7b12015-11-26 19:51:21 -0700542 *
543 * With driver model this is dev_get_uclass_priv(bus)
wdenkc6097192002-11-03 00:24:07 +0000544 */
545struct pci_controller {
Simon Glassff3e0772015-03-05 12:25:25 -0700546#ifdef CONFIG_DM_PCI
547 struct udevice *bus;
548 struct udevice *ctlr;
549#else
wdenkc6097192002-11-03 00:24:07 +0000550 struct pci_controller *next;
Simon Glassff3e0772015-03-05 12:25:25 -0700551#endif
wdenkc6097192002-11-03 00:24:07 +0000552
553 int first_busno;
554 int last_busno;
555
556 volatile unsigned int *cfg_addr;
557 volatile unsigned char *cfg_data;
558
Anton Vorontsovfd6646c2009-01-08 04:26:12 +0300559 int indirect_type;
560
Simon Glassaec241d2015-06-07 08:50:40 -0600561 /*
562 * TODO(sjg@chromium.org): With driver model we use struct
563 * pci_controller for both the controller and any bridge devices
564 * attached to it. But there is only one region list and it is in the
565 * top-level controller.
566 *
567 * This could be changed so that struct pci_controller is only used
568 * for PCI controllers and a separate UCLASS (or perhaps
569 * UCLASS_PCI_GENERIC) is used for bridges.
570 */
wdenkc6097192002-11-03 00:24:07 +0000571 struct pci_region regions[MAX_PCI_REGIONS];
572 int region_count;
573
574 struct pci_config_table *config_table;
575
576 void (*fixup_irq)(struct pci_controller *, pci_dev_t);
Simon Glassff3e0772015-03-05 12:25:25 -0700577#ifndef CONFIG_DM_PCI
wdenkc6097192002-11-03 00:24:07 +0000578 /* Low-level architecture-dependent routines */
579 int (*read_byte)(struct pci_controller*, pci_dev_t, int where, u8 *);
580 int (*read_word)(struct pci_controller*, pci_dev_t, int where, u16 *);
581 int (*read_dword)(struct pci_controller*, pci_dev_t, int where, u32 *);
582 int (*write_byte)(struct pci_controller*, pci_dev_t, int where, u8);
583 int (*write_word)(struct pci_controller*, pci_dev_t, int where, u16);
584 int (*write_dword)(struct pci_controller*, pci_dev_t, int where, u32);
Simon Glassff3e0772015-03-05 12:25:25 -0700585#endif
wdenkc6097192002-11-03 00:24:07 +0000586
587 /* Used by auto config */
Kumar Galaa1790122006-01-11 13:24:15 -0600588 struct pci_region *pci_mem, *pci_io, *pci_prefetch;
wdenkc6097192002-11-03 00:24:07 +0000589
Simon Glassff3e0772015-03-05 12:25:25 -0700590#ifndef CONFIG_DM_PCI
wdenkc7de8292002-11-19 11:04:11 +0000591 int current_busno;
Leo Liu10fa8d72011-01-19 19:50:47 +0800592
593 void *priv_data;
Simon Glassff3e0772015-03-05 12:25:25 -0700594#endif
wdenkc6097192002-11-03 00:24:07 +0000595};
596
Simon Glassff3e0772015-03-05 12:25:25 -0700597#ifndef CONFIG_DM_PCI
Simon Glassbc3442a2013-06-11 11:14:33 -0700598static inline void pci_set_ops(struct pci_controller *hose,
wdenkc6097192002-11-03 00:24:07 +0000599 int (*read_byte)(struct pci_controller*,
600 pci_dev_t, int where, u8 *),
601 int (*read_word)(struct pci_controller*,
602 pci_dev_t, int where, u16 *),
603 int (*read_dword)(struct pci_controller*,
604 pci_dev_t, int where, u32 *),
605 int (*write_byte)(struct pci_controller*,
606 pci_dev_t, int where, u8),
607 int (*write_word)(struct pci_controller*,
608 pci_dev_t, int where, u16),
609 int (*write_dword)(struct pci_controller*,
610 pci_dev_t, int where, u32)) {
611 hose->read_byte = read_byte;
612 hose->read_word = read_word;
613 hose->read_dword = read_dword;
614 hose->write_byte = write_byte;
615 hose->write_word = write_word;
616 hose->write_dword = write_dword;
617}
Simon Glassff3e0772015-03-05 12:25:25 -0700618#endif
wdenkc6097192002-11-03 00:24:07 +0000619
Gabor Juhos842033e2013-05-30 07:06:12 +0000620#ifdef CONFIG_PCI_INDIRECT_BRIDGE
wdenkc6097192002-11-03 00:24:07 +0000621extern void pci_setup_indirect(struct pci_controller* hose, u32 cfg_addr, u32 cfg_data);
Gabor Juhos842033e2013-05-30 07:06:12 +0000622#endif
wdenkc6097192002-11-03 00:24:07 +0000623
Simon Glass7e78b9e2015-11-29 13:18:05 -0700624#if !defined(CONFIG_DM_PCI) || defined(CONFIG_DM_PCI_COMPAT)
Becky Bruce36f32672008-05-07 13:24:57 -0500625extern phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose,
Kumar Gala30e76d52008-10-21 08:36:08 -0500626 pci_addr_t addr, unsigned long flags);
627extern pci_addr_t pci_hose_phys_to_bus(struct pci_controller* hose,
628 phys_addr_t addr, unsigned long flags);
wdenkc6097192002-11-03 00:24:07 +0000629
630#define pci_phys_to_bus(dev, addr, flags) \
631 pci_hose_phys_to_bus(pci_bus_to_hose(PCI_BUS(dev)), (addr), (flags))
632#define pci_bus_to_phys(dev, addr, flags) \
633 pci_hose_bus_to_phys(pci_bus_to_hose(PCI_BUS(dev)), (addr), (flags))
634
Becky Bruce6e61fae2009-02-03 18:10:50 -0600635#define pci_virt_to_bus(dev, addr, flags) \
636 pci_hose_phys_to_bus(pci_bus_to_hose(PCI_BUS(dev)), \
637 (virt_to_phys(addr)), (flags))
638#define pci_bus_to_virt(dev, addr, flags, len, map_flags) \
639 map_physmem(pci_hose_bus_to_phys(pci_bus_to_hose(PCI_BUS(dev)), \
640 (addr), (flags)), \
641 (len), (map_flags))
642
643#define pci_phys_to_mem(dev, addr) \
644 pci_phys_to_bus((dev), (addr), PCI_REGION_MEM)
645#define pci_mem_to_phys(dev, addr) \
646 pci_bus_to_phys((dev), (addr), PCI_REGION_MEM)
647#define pci_phys_to_io(dev, addr) pci_phys_to_bus((dev), (addr), PCI_REGION_IO)
648#define pci_io_to_phys(dev, addr) pci_bus_to_phys((dev), (addr), PCI_REGION_IO)
649
650#define pci_virt_to_mem(dev, addr) \
651 pci_virt_to_bus((dev), (addr), PCI_REGION_MEM)
652#define pci_mem_to_virt(dev, addr, len, map_flags) \
653 pci_bus_to_virt((dev), (addr), PCI_REGION_MEM, (len), (map_flags))
654#define pci_virt_to_io(dev, addr) \
655 pci_virt_to_bus((dev), (addr), PCI_REGION_IO)
656#define pci_io_to_virt(dev, addr, len, map_flags) \
657 pci_bus_to_virt((dev), (addr), PCI_REGION_IO, (len), (map_flags))
wdenkc6097192002-11-03 00:24:07 +0000658
Simon Glassdc5740d2015-08-22 15:58:55 -0600659/* For driver model these are defined in macros in pci_compat.c */
wdenkc6097192002-11-03 00:24:07 +0000660extern int pci_hose_read_config_byte(struct pci_controller *hose,
661 pci_dev_t dev, int where, u8 *val);
662extern int pci_hose_read_config_word(struct pci_controller *hose,
663 pci_dev_t dev, int where, u16 *val);
664extern int pci_hose_read_config_dword(struct pci_controller *hose,
665 pci_dev_t dev, int where, u32 *val);
666extern int pci_hose_write_config_byte(struct pci_controller *hose,
667 pci_dev_t dev, int where, u8 val);
668extern int pci_hose_write_config_word(struct pci_controller *hose,
669 pci_dev_t dev, int where, u16 val);
670extern int pci_hose_write_config_dword(struct pci_controller *hose,
671 pci_dev_t dev, int where, u32 val);
Simon Glass3ba5f742015-11-26 19:51:30 -0700672#endif
wdenkc6097192002-11-03 00:24:07 +0000673
Simon Glassff3e0772015-03-05 12:25:25 -0700674#ifndef CONFIG_DM_PCI
wdenkc6097192002-11-03 00:24:07 +0000675extern int pci_read_config_byte(pci_dev_t dev, int where, u8 *val);
676extern int pci_read_config_word(pci_dev_t dev, int where, u16 *val);
677extern int pci_read_config_dword(pci_dev_t dev, int where, u32 *val);
678extern int pci_write_config_byte(pci_dev_t dev, int where, u8 val);
679extern int pci_write_config_word(pci_dev_t dev, int where, u16 val);
680extern int pci_write_config_dword(pci_dev_t dev, int where, u32 val);
Simon Glassff3e0772015-03-05 12:25:25 -0700681#endif
wdenkc6097192002-11-03 00:24:07 +0000682
Simon Glass3ba5f742015-11-26 19:51:30 -0700683void pciauto_region_init(struct pci_region *res);
684void pciauto_region_align(struct pci_region *res, pci_size_t size);
685void pciauto_config_init(struct pci_controller *hose);
Tuomas Tynkkynen5ce9aca2018-05-14 23:50:05 +0300686
687/**
688 * pciauto_region_allocate() - Allocate resources from a PCI resource region
689 *
690 * Allocates @size bytes from the PCI resource @res. If @supports_64bit is
691 * false, the result will be guaranteed to fit in 32 bits.
692 *
693 * @res: PCI region to allocate from
694 * @size: Amount of bytes to allocate
695 * @bar: Returns the PCI bus address of the allocated resource
696 * @supports_64bit: Whether to allow allocations above the 32-bit boundary
697 * @return 0 if successful, -1 on failure
698 */
Simon Glass3ba5f742015-11-26 19:51:30 -0700699int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
Tuomas Tynkkynend71975a2018-05-14 19:38:13 +0300700 pci_addr_t *bar, bool supports_64bit);
Simon Glass3ba5f742015-11-26 19:51:30 -0700701
702#if !defined(CONFIG_DM_PCI) || defined(CONFIG_DM_PCI_COMPAT)
wdenkc6097192002-11-03 00:24:07 +0000703extern int pci_hose_read_config_byte_via_dword(struct pci_controller *hose,
704 pci_dev_t dev, int where, u8 *val);
705extern int pci_hose_read_config_word_via_dword(struct pci_controller *hose,
706 pci_dev_t dev, int where, u16 *val);
707extern int pci_hose_write_config_byte_via_dword(struct pci_controller *hose,
708 pci_dev_t dev, int where, u8 val);
709extern int pci_hose_write_config_word_via_dword(struct pci_controller *hose,
710 pci_dev_t dev, int where, u16 val);
711
Becky Bruce6e61fae2009-02-03 18:10:50 -0600712extern void *pci_map_bar(pci_dev_t pdev, int bar, int flags);
wdenkc6097192002-11-03 00:24:07 +0000713extern void pci_register_hose(struct pci_controller* hose);
714extern struct pci_controller* pci_bus_to_hose(int bus);
Kumar Gala3a0e3c22010-12-17 05:57:25 -0600715extern struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr);
Stuart Yodereeb5b1a2016-03-10 10:52:18 -0600716extern struct pci_controller *pci_get_hose_head(void);
wdenkc6097192002-11-03 00:24:07 +0000717
Thierry Reding4efe52b2014-11-12 18:26:49 -0700718extern int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev);
wdenkc6097192002-11-03 00:24:07 +0000719extern int pci_hose_scan(struct pci_controller *hose);
720extern int pci_hose_scan_bus(struct pci_controller *hose, int bus);
721
wdenkc6097192002-11-03 00:24:07 +0000722extern void pciauto_setup_device(struct pci_controller *hose,
723 pci_dev_t dev, int bars_num,
724 struct pci_region *mem,
Kumar Galaa1790122006-01-11 13:24:15 -0600725 struct pci_region *prefetch,
wdenkc6097192002-11-03 00:24:07 +0000726 struct pci_region *io);
Linus Walleija3a70722012-03-25 12:13:15 +0000727extern void pciauto_prescan_setup_bridge(struct pci_controller *hose,
728 pci_dev_t dev, int sub_bus);
729extern void pciauto_postscan_setup_bridge(struct pci_controller *hose,
730 pci_dev_t dev, int sub_bus);
Linus Walleija3a70722012-03-25 12:13:15 +0000731extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
wdenkc6097192002-11-03 00:24:07 +0000732
733extern pci_dev_t pci_find_device (unsigned int vendor, unsigned int device, int index);
734extern pci_dev_t pci_find_devices (struct pci_device_id *ids, int index);
Simon Glass250e0392015-01-27 22:13:27 -0700735pci_dev_t pci_find_class(unsigned int find_class, int index);
wdenkc6097192002-11-03 00:24:07 +0000736
737extern int pci_hose_config_device(struct pci_controller *hose,
738 pci_dev_t dev,
739 unsigned long io,
Kumar Gala30e76d52008-10-21 08:36:08 -0500740 pci_addr_t mem,
wdenkc6097192002-11-03 00:24:07 +0000741 unsigned long command);
742
Zhao Qiang287df012013-10-12 13:46:33 +0800743extern int pci_hose_find_capability(struct pci_controller *hose, pci_dev_t dev,
744 int cap);
745extern int pci_hose_find_cap_start(struct pci_controller *hose, pci_dev_t dev,
746 u8 hdr_type);
747extern int pci_find_cap(struct pci_controller *hose, pci_dev_t dev, int pos,
748 int cap);
749
Minghuan Lianed5b5802015-07-10 11:35:08 +0800750int pci_find_next_ext_capability(struct pci_controller *hose,
751 pci_dev_t dev, int start, int cap);
752int pci_hose_find_ext_capability(struct pci_controller *hose,
753 pci_dev_t dev, int cap);
754
Tim Harvey09918662014-08-07 22:49:56 -0700755#ifdef CONFIG_PCI_FIXUP_DEV
756extern void board_pci_fixup_dev(struct pci_controller *hose, pci_dev_t dev,
757 unsigned short vendor,
758 unsigned short device,
759 unsigned short class);
760#endif
Simon Glass3ba5f742015-11-26 19:51:30 -0700761#endif /* !defined(CONFIG_DM_PCI) || defined(CONFIG_DM_PCI_COMPAT) */
Tim Harvey09918662014-08-07 22:49:56 -0700762
Peter Tyser983eb9d2010-10-29 17:59:27 -0500763const char * pci_class_str(u8 class);
Anton Vorontsovcc2a8c72009-02-19 18:20:41 +0300764int pci_last_busno(void);
765
Jon Loeliger13a7fcd2006-10-19 11:33:52 -0500766#ifdef CONFIG_MPC85xx
767extern void pci_mpc85xx_init (struct pci_controller *hose);
768#endif
Paul Burtonfa5cec02013-11-08 11:18:47 +0000769
Tim Harvey6ecbe132017-05-12 12:58:41 -0700770#ifdef CONFIG_PCIE_IMX
771extern void imx_pcie_remove(void);
772#endif
773
Simon Glass3ba5f742015-11-26 19:51:30 -0700774#if !defined(CONFIG_DM_PCI) || defined(CONFIG_DM_PCI_COMPAT)
Simon Glasse8a552e2014-11-14 18:18:30 -0700775/**
776 * pci_write_bar32() - Write the address of a BAR including control bits
777 *
Simon Glass9d731c82016-01-18 20:19:15 -0700778 * This writes a raw address (with control bits) to a bar. This can be used
779 * with devices which require hard-coded addresses, not part of the normal
780 * PCI enumeration process.
Simon Glasse8a552e2014-11-14 18:18:30 -0700781 *
782 * @hose: PCI hose to use
783 * @dev: PCI device to update
784 * @barnum: BAR number (0-5)
785 * @addr: BAR address with control bits
786 */
787void pci_write_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum,
Simon Glass9d731c82016-01-18 20:19:15 -0700788 u32 addr);
Simon Glasse8a552e2014-11-14 18:18:30 -0700789
790/**
791 * pci_read_bar32() - read the address of a bar
792 *
793 * @hose: PCI hose to use
794 * @dev: PCI device to inspect
795 * @barnum: BAR number (0-5)
796 * @return address of the bar, masking out any control bits
797 * */
798u32 pci_read_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum);
799
Simon Glass4a2708a2015-01-14 21:37:04 -0700800/**
Simon Glassaab67242015-03-05 12:25:24 -0700801 * pci_hose_find_devices() - Find devices by vendor/device ID
802 *
803 * @hose: PCI hose to search
804 * @busnum: Bus number to search
805 * @ids: PCI vendor/device IDs to look for, terminated by 0, 0 record
806 * @indexp: Pointer to device index to find. To find the first matching
807 * device, pass 0; to find the second, pass 1, etc. This
808 * parameter is decremented for each non-matching device so
809 * can be called repeatedly.
810 */
811pci_dev_t pci_hose_find_devices(struct pci_controller *hose, int busnum,
812 struct pci_device_id *ids, int *indexp);
Simon Glass3ba5f742015-11-26 19:51:30 -0700813#endif /* !CONFIG_DM_PCI || CONFIG_DM_PCI_COMPAT */
Simon Glassaab67242015-03-05 12:25:24 -0700814
Simon Glassff3e0772015-03-05 12:25:25 -0700815/* Access sizes for PCI reads and writes */
816enum pci_size_t {
817 PCI_SIZE_8,
818 PCI_SIZE_16,
819 PCI_SIZE_32,
820};
821
822struct udevice;
823
824#ifdef CONFIG_DM_PCI
825/**
826 * struct pci_child_platdata - information stored about each PCI device
827 *
828 * Every device on a PCI bus has this per-child data.
829 *
Simon Glassbcbe3d12015-09-28 23:32:01 -0600830 * It can be accessed using dev_get_parent_priv(dev) if dev->parent is a
Simon Glassff3e0772015-03-05 12:25:25 -0700831 * PCI bus (i.e. UCLASS_PCI)
832 *
833 * @devfn: Encoded device and function index - see PCI_DEVFN()
834 * @vendor: PCI vendor ID (see pci_ids.h)
835 * @device: PCI device ID (see pci_ids.h)
836 * @class: PCI class, 3 bytes: (base, sub, prog-if)
837 */
838struct pci_child_platdata {
839 int devfn;
840 unsigned short vendor;
841 unsigned short device;
842 unsigned int class;
843};
844
845/* PCI bus operations */
846struct dm_pci_ops {
847 /**
848 * read_config() - Read a PCI configuration value
849 *
850 * PCI buses must support reading and writing configuration values
851 * so that the bus can be scanned and its devices configured.
852 *
853 * Normally PCI_BUS(@bdf) is the same as @bus->seq, but not always.
854 * If bridges exist it is possible to use the top-level bus to
855 * access a sub-bus. In that case @bus will be the top-level bus
856 * and PCI_BUS(bdf) will be a different (higher) value
857 *
858 * @bus: Bus to read from
859 * @bdf: Bus, device and function to read
860 * @offset: Byte offset within the device's configuration space
861 * @valuep: Place to put the returned value
862 * @size: Access size
863 * @return 0 if OK, -ve on error
864 */
865 int (*read_config)(struct udevice *bus, pci_dev_t bdf, uint offset,
866 ulong *valuep, enum pci_size_t size);
867 /**
868 * write_config() - Write a PCI configuration value
869 *
870 * @bus: Bus to write to
871 * @bdf: Bus, device and function to write
872 * @offset: Byte offset within the device's configuration space
873 * @value: Value to write
874 * @size: Access size
875 * @return 0 if OK, -ve on error
876 */
877 int (*write_config)(struct udevice *bus, pci_dev_t bdf, uint offset,
878 ulong value, enum pci_size_t size);
879};
880
881/* Get access to a PCI bus' operations */
882#define pci_get_ops(dev) ((struct dm_pci_ops *)(dev)->driver->ops)
883
884/**
Simon Glass21ccce12015-11-29 13:17:47 -0700885 * dm_pci_get_bdf() - Get the BDF value for a device
Simon Glass4b515e42015-07-06 16:47:46 -0600886 *
887 * @dev: Device to check
888 * @return bus/device/function value (see PCI_BDF())
889 */
Simon Glass21ccce12015-11-29 13:17:47 -0700890pci_dev_t dm_pci_get_bdf(struct udevice *dev);
Simon Glass4b515e42015-07-06 16:47:46 -0600891
892/**
Simon Glassff3e0772015-03-05 12:25:25 -0700893 * pci_bind_bus_devices() - scan a PCI bus and bind devices
894 *
895 * Scan a PCI bus looking for devices. Bind each one that is found. If
896 * devices are already bound that match the scanned devices, just update the
897 * child data so that the device can be used correctly (this happens when
898 * the device tree describes devices we expect to see on the bus).
899 *
900 * Devices that are bound in this way will use a generic PCI driver which
901 * does nothing. The device can still be accessed but will not provide any
902 * driver interface.
903 *
904 * @bus: Bus containing devices to bind
905 * @return 0 if OK, -ve on error
906 */
907int pci_bind_bus_devices(struct udevice *bus);
908
909/**
910 * pci_auto_config_devices() - configure bus devices ready for use
911 *
912 * This works through all devices on a bus by scanning the driver model
913 * data structures (normally these have been set up by pci_bind_bus_devices()
914 * earlier).
915 *
916 * Space is allocated for each PCI base address register (BAR) so that the
917 * devices are mapped into memory and I/O space ready for use.
918 *
919 * @bus: Bus containing devices to bind
920 * @return 0 if OK, -ve on error
921 */
922int pci_auto_config_devices(struct udevice *bus);
923
924/**
Simon Glassf3f1fae2015-11-29 13:17:48 -0700925 * dm_pci_bus_find_bdf() - Find a device given its PCI bus address
Simon Glassff3e0772015-03-05 12:25:25 -0700926 *
927 * @bdf: PCI device address: bus, device and function -see PCI_BDF()
928 * @devp: Returns the device for this address, if found
929 * @return 0 if OK, -ENODEV if not found
930 */
Simon Glassf3f1fae2015-11-29 13:17:48 -0700931int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp);
Simon Glassff3e0772015-03-05 12:25:25 -0700932
933/**
934 * pci_bus_find_devfn() - Find a device on a bus
935 *
936 * @find_devfn: PCI device address (device and function only)
937 * @devp: Returns the device for this address, if found
938 * @return 0 if OK, -ENODEV if not found
939 */
940int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
941 struct udevice **devp);
942
943/**
Simon Glass76c3fbc2015-08-10 07:05:04 -0600944 * pci_find_first_device() - return the first available PCI device
945 *
946 * This function and pci_find_first_device() allow iteration through all
947 * available PCI devices on all buses. Assuming there are any, this will
948 * return the first one.
949 *
950 * @devp: Set to the first available device, or NULL if no more are left
951 * or we got an error
952 * @return 0 if all is OK, -ve on error (e.g. a bus/bridge failed to probe)
953 */
954int pci_find_first_device(struct udevice **devp);
955
956/**
957 * pci_find_next_device() - return the next available PCI device
958 *
959 * Finds the next available PCI device after the one supplied, or sets @devp
960 * to NULL if there are no more.
961 *
962 * @devp: On entry, the last device returned. Set to the next available
963 * device, or NULL if no more are left or we got an error
964 * @return 0 if all is OK, -ve on error (e.g. a bus/bridge failed to probe)
965 */
966int pci_find_next_device(struct udevice **devp);
967
968/**
Simon Glassff3e0772015-03-05 12:25:25 -0700969 * pci_get_ff() - Returns a mask for the given access size
970 *
971 * @size: Access size
972 * @return 0xff for PCI_SIZE_8, 0xffff for PCI_SIZE_16, 0xffffffff for
973 * PCI_SIZE_32
974 */
975int pci_get_ff(enum pci_size_t size);
976
977/**
978 * pci_bus_find_devices () - Find devices on a bus
979 *
980 * @bus: Bus to search
981 * @ids: PCI vendor/device IDs to look for, terminated by 0, 0 record
982 * @indexp: Pointer to device index to find. To find the first matching
983 * device, pass 0; to find the second, pass 1, etc. This
984 * parameter is decremented for each non-matching device so
985 * can be called repeatedly.
986 * @devp: Returns matching device if found
987 * @return 0 if found, -ENODEV if not
988 */
989int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
990 int *indexp, struct udevice **devp);
991
992/**
993 * pci_find_device_id() - Find a device on any bus
994 *
995 * @ids: PCI vendor/device IDs to look for, terminated by 0, 0 record
996 * @index: Index number of device to find, 0 for the first match, 1 for
997 * the second, etc.
998 * @devp: Returns matching device if found
999 * @return 0 if found, -ENODEV if not
1000 */
1001int pci_find_device_id(struct pci_device_id *ids, int index,
1002 struct udevice **devp);
1003
1004/**
1005 * dm_pci_hose_probe_bus() - probe a subordinate bus, scanning it for devices
1006 *
1007 * This probes the given bus which causes it to be scanned for devices. The
1008 * devices will be bound but not probed.
1009 *
1010 * @hose specifies the PCI hose that will be used for the scan. This is
1011 * always a top-level bus with uclass UCLASS_PCI. The bus to scan is
1012 * in @bdf, and is a subordinate bus reachable from @hose.
1013 *
1014 * @hose: PCI hose to scan
1015 * @bdf: PCI bus address to scan (PCI_BUS(bdf) is the bus number)
1016 * @return 0 if OK, -ve on error
1017 */
Simon Glass5e23b8b2015-11-29 13:17:49 -07001018int dm_pci_hose_probe_bus(struct udevice *bus);
Simon Glassff3e0772015-03-05 12:25:25 -07001019
1020/**
1021 * pci_bus_read_config() - Read a configuration value from a device
1022 *
1023 * TODO(sjg@chromium.org): We should be able to pass just a device and have
1024 * it do the right thing. It would be good to have that function also.
1025 *
1026 * @bus: Bus to read from
1027 * @bdf: PCI device address: bus, device and function -see PCI_BDF()
Simon Glass4974a6f2016-03-06 19:27:53 -07001028 * @offset: Register offset to read
Simon Glassff3e0772015-03-05 12:25:25 -07001029 * @valuep: Place to put the returned value
1030 * @size: Access size
1031 * @return 0 if OK, -ve on error
1032 */
1033int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
1034 unsigned long *valuep, enum pci_size_t size);
1035
1036/**
1037 * pci_bus_write_config() - Write a configuration value to a device
1038 *
1039 * @bus: Bus to write from
1040 * @bdf: PCI device address: bus, device and function -see PCI_BDF()
Simon Glass4974a6f2016-03-06 19:27:53 -07001041 * @offset: Register offset to write
Simon Glassff3e0772015-03-05 12:25:25 -07001042 * @value: Value to write
1043 * @size: Access size
1044 * @return 0 if OK, -ve on error
1045 */
1046int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
1047 unsigned long value, enum pci_size_t size);
1048
Simon Glass66afb4e2015-08-10 07:05:03 -06001049/**
Simon Glass319dba12016-03-06 19:27:52 -07001050 * pci_bus_clrset_config32() - Update a configuration value for a device
1051 *
1052 * The register at @offset is updated to (oldvalue & ~clr) | set.
1053 *
1054 * @bus: Bus to access
1055 * @bdf: PCI device address: bus, device and function -see PCI_BDF()
1056 * @offset: Register offset to update
1057 * @clr: Bits to clear
1058 * @set: Bits to set
1059 * @return 0 if OK, -ve on error
1060 */
1061int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
1062 u32 clr, u32 set);
1063
1064/**
Simon Glass66afb4e2015-08-10 07:05:03 -06001065 * Driver model PCI config access functions. Use these in preference to others
1066 * when you have a valid device
1067 */
1068int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
1069 enum pci_size_t size);
1070
1071int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep);
1072int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep);
1073int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep);
1074
1075int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
1076 enum pci_size_t size);
1077
1078int dm_pci_write_config8(struct udevice *dev, int offset, u8 value);
1079int dm_pci_write_config16(struct udevice *dev, int offset, u16 value);
1080int dm_pci_write_config32(struct udevice *dev, int offset, u32 value);
1081
Simon Glass319dba12016-03-06 19:27:52 -07001082/**
1083 * These permit convenient read/modify/write on PCI configuration. The
1084 * register is updated to (oldvalue & ~clr) | set.
1085 */
1086int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set);
1087int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set);
1088int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set);
1089
Simon Glassff3e0772015-03-05 12:25:25 -07001090/*
1091 * The following functions provide access to the above without needing the
1092 * size parameter. We are trying to encourage the use of the 8/16/32-style
1093 * functions, rather than byte/word/dword. But both are supported.
1094 */
1095int pci_write_config32(pci_dev_t pcidev, int offset, u32 value);
Bin Meng308143e2016-02-02 05:58:07 -08001096int pci_write_config16(pci_dev_t pcidev, int offset, u16 value);
1097int pci_write_config8(pci_dev_t pcidev, int offset, u8 value);
1098int pci_read_config32(pci_dev_t pcidev, int offset, u32 *valuep);
1099int pci_read_config16(pci_dev_t pcidev, int offset, u16 *valuep);
1100int pci_read_config8(pci_dev_t pcidev, int offset, u8 *valuep);
Simon Glassff3e0772015-03-05 12:25:25 -07001101
Tuomas Tynkkynenbadb9922017-09-19 23:18:03 +03001102/**
1103 * pci_generic_mmap_write_config() - Generic helper for writing to
1104 * memory-mapped PCI configuration space.
1105 * @bus: Pointer to the PCI bus
1106 * @addr_f: Callback for calculating the config space address
1107 * @bdf: Identifies the PCI device to access
1108 * @offset: The offset into the device's configuration space
1109 * @value: The value to write
1110 * @size: Indicates the size of access to perform
1111 *
1112 * Write the value @value of size @size from offset @offset within the
1113 * configuration space of the device identified by the bus, device & function
1114 * numbers in @bdf on the PCI bus @bus. The callback function @addr_f is
1115 * responsible for calculating the CPU address of the respective configuration
1116 * space offset.
1117 *
1118 * Return: 0 on success, else -EINVAL
1119 */
1120int pci_generic_mmap_write_config(
1121 struct udevice *bus,
1122 int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
1123 pci_dev_t bdf,
1124 uint offset,
1125 ulong value,
1126 enum pci_size_t size);
1127
1128/**
1129 * pci_generic_mmap_read_config() - Generic helper for reading from
1130 * memory-mapped PCI configuration space.
1131 * @bus: Pointer to the PCI bus
1132 * @addr_f: Callback for calculating the config space address
1133 * @bdf: Identifies the PCI device to access
1134 * @offset: The offset into the device's configuration space
1135 * @valuep: A pointer at which to store the read value
1136 * @size: Indicates the size of access to perform
1137 *
1138 * Read a value of size @size from offset @offset within the configuration
1139 * space of the device identified by the bus, device & function numbers in @bdf
1140 * on the PCI bus @bus. The callback function @addr_f is responsible for
1141 * calculating the CPU address of the respective configuration space offset.
1142 *
1143 * Return: 0 on success, else -EINVAL
1144 */
1145int pci_generic_mmap_read_config(
1146 struct udevice *bus,
1147 int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
1148 pci_dev_t bdf,
1149 uint offset,
1150 ulong *valuep,
1151 enum pci_size_t size);
1152
Simon Glass3ba5f742015-11-26 19:51:30 -07001153#ifdef CONFIG_DM_PCI_COMPAT
Simon Glassff3e0772015-03-05 12:25:25 -07001154/* Compatibility with old naming */
1155static inline int pci_write_config_dword(pci_dev_t pcidev, int offset,
1156 u32 value)
1157{
1158 return pci_write_config32(pcidev, offset, value);
1159}
1160
Simon Glassff3e0772015-03-05 12:25:25 -07001161/* Compatibility with old naming */
1162static inline int pci_write_config_word(pci_dev_t pcidev, int offset,
1163 u16 value)
1164{
1165 return pci_write_config16(pcidev, offset, value);
1166}
1167
Simon Glassff3e0772015-03-05 12:25:25 -07001168/* Compatibility with old naming */
1169static inline int pci_write_config_byte(pci_dev_t pcidev, int offset,
1170 u8 value)
1171{
1172 return pci_write_config8(pcidev, offset, value);
1173}
1174
Simon Glassff3e0772015-03-05 12:25:25 -07001175/* Compatibility with old naming */
1176static inline int pci_read_config_dword(pci_dev_t pcidev, int offset,
1177 u32 *valuep)
1178{
1179 return pci_read_config32(pcidev, offset, valuep);
1180}
1181
Simon Glassff3e0772015-03-05 12:25:25 -07001182/* Compatibility with old naming */
1183static inline int pci_read_config_word(pci_dev_t pcidev, int offset,
1184 u16 *valuep)
1185{
1186 return pci_read_config16(pcidev, offset, valuep);
1187}
1188
Simon Glassff3e0772015-03-05 12:25:25 -07001189/* Compatibility with old naming */
1190static inline int pci_read_config_byte(pci_dev_t pcidev, int offset,
1191 u8 *valuep)
1192{
1193 return pci_read_config8(pcidev, offset, valuep);
1194}
Simon Glass3ba5f742015-11-26 19:51:30 -07001195#endif /* CONFIG_DM_PCI_COMPAT */
1196
1197/**
1198 * dm_pciauto_config_device() - configure a device ready for use
1199 *
1200 * Space is allocated for each PCI base address register (BAR) so that the
1201 * devices are mapped into memory and I/O space ready for use.
1202 *
1203 * @dev: Device to configure
1204 * @return 0 if OK, -ve on error
1205 */
1206int dm_pciauto_config_device(struct udevice *dev);
1207
Simon Glass36d0d3b2015-03-05 12:25:28 -07001208/**
Simon Glass9289db62015-11-19 20:26:59 -07001209 * pci_conv_32_to_size() - convert a 32-bit read value to the given size
1210 *
1211 * Some PCI buses must always perform 32-bit reads. The data must then be
1212 * shifted and masked to reflect the required access size and offset. This
1213 * function performs this transformation.
1214 *
1215 * @value: Value to transform (32-bit value read from @offset & ~3)
1216 * @offset: Register offset that was read
1217 * @size: Required size of the result
1218 * @return the value that would have been obtained if the read had been
1219 * performed at the given offset with the correct size
1220 */
1221ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size);
1222
1223/**
1224 * pci_conv_size_to_32() - update a 32-bit value to prepare for a write
1225 *
1226 * Some PCI buses must always perform 32-bit writes. To emulate a smaller
1227 * write the old 32-bit data must be read, updated with the required new data
1228 * and written back as a 32-bit value. This function performs the
1229 * transformation from the old value to the new value.
1230 *
1231 * @value: Value to transform (32-bit value read from @offset & ~3)
1232 * @offset: Register offset that should be written
1233 * @size: Required size of the write
1234 * @return the value that should be written as a 32-bit access to @offset & ~3.
1235 */
1236ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1237 enum pci_size_t size);
1238
1239/**
Simon Glass9f60fb02015-11-19 20:27:00 -07001240 * pci_get_controller() - obtain the controller to use for a bus
1241 *
1242 * @dev: Device to check
1243 * @return pointer to the controller device for this bus
1244 */
1245struct udevice *pci_get_controller(struct udevice *dev);
1246
1247/**
Simon Glassf9260332015-11-19 20:27:01 -07001248 * pci_get_regions() - obtain pointers to all the region types
1249 *
1250 * @dev: Device to check
1251 * @iop: Returns a pointer to the I/O region, or NULL if none
1252 * @memp: Returns a pointer to the memory region, or NULL if none
1253 * @prefp: Returns a pointer to the pre-fetch region, or NULL if none
1254 * @return the number of non-NULL regions returned, normally 3
1255 */
1256int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1257 struct pci_region **memp, struct pci_region **prefp);
1258
1259/**
Simon Glass9d731c82016-01-18 20:19:15 -07001260 * dm_pci_write_bar32() - Write the address of a BAR
1261 *
1262 * This writes a raw address to a bar
1263 *
1264 * @dev: PCI device to update
1265 * @barnum: BAR number (0-5)
1266 * @addr: BAR address
1267 */
1268void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr);
1269
1270/**
Simon Glassbab17cf2015-11-29 13:17:53 -07001271 * dm_pci_read_bar32() - read a base address register from a device
1272 *
1273 * @dev: Device to check
1274 * @barnum: Bar number to read (numbered from 0)
1275 * @return: value of BAR
1276 */
1277u32 dm_pci_read_bar32(struct udevice *dev, int barnum);
1278
1279/**
Simon Glass21d1fe72015-11-29 13:18:03 -07001280 * dm_pci_bus_to_phys() - convert a PCI bus address to a physical address
1281 *
1282 * @dev: Device containing the PCI address
1283 * @addr: PCI address to convert
1284 * @flags: Flags for the region type (PCI_REGION_...)
1285 * @return physical address corresponding to that PCI bus address
1286 */
1287phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t addr,
1288 unsigned long flags);
1289
1290/**
1291 * dm_pci_phys_to_bus() - convert a physical address to a PCI bus address
1292 *
1293 * @dev: Device containing the bus address
1294 * @addr: Physical address to convert
1295 * @flags: Flags for the region type (PCI_REGION_...)
1296 * @return PCI bus address corresponding to that physical address
1297 */
1298pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t addr,
1299 unsigned long flags);
1300
1301/**
1302 * dm_pci_map_bar() - get a virtual address associated with a BAR region
1303 *
1304 * Looks up a base address register and finds the physical memory address
1305 * that corresponds to it
1306 *
1307 * @dev: Device to check
1308 * @bar: Bar number to read (numbered from 0)
1309 * @flags: Flags for the region type (PCI_REGION_...)
1310 * @return: pointer to the virtual address to use
1311 */
1312void *dm_pci_map_bar(struct udevice *dev, int bar, int flags);
1313
1314#define dm_pci_virt_to_bus(dev, addr, flags) \
1315 dm_pci_phys_to_bus(dev, (virt_to_phys(addr)), (flags))
1316#define dm_pci_bus_to_virt(dev, addr, flags, len, map_flags) \
1317 map_physmem(dm_pci_bus_to_phys(dev, (addr), (flags)), \
1318 (len), (map_flags))
1319
1320#define dm_pci_phys_to_mem(dev, addr) \
1321 dm_pci_phys_to_bus((dev), (addr), PCI_REGION_MEM)
1322#define dm_pci_mem_to_phys(dev, addr) \
1323 dm_pci_bus_to_phys((dev), (addr), PCI_REGION_MEM)
1324#define dm_pci_phys_to_io(dev, addr) \
1325 dm_pci_phys_to_bus((dev), (addr), PCI_REGION_IO)
1326#define dm_pci_io_to_phys(dev, addr) \
1327 dm_pci_bus_to_phys((dev), (addr), PCI_REGION_IO)
1328
1329#define dm_pci_virt_to_mem(dev, addr) \
1330 dm_pci_virt_to_bus((dev), (addr), PCI_REGION_MEM)
1331#define dm_pci_mem_to_virt(dev, addr, len, map_flags) \
1332 dm_pci_bus_to_virt((dev), (addr), PCI_REGION_MEM, (len), (map_flags))
1333#define dm_pci_virt_to_io(dev, addr) \
Simon Glass4974a6f2016-03-06 19:27:53 -07001334 dm_pci_virt_to_bus((dev), (addr), PCI_REGION_IO)
Simon Glass21d1fe72015-11-29 13:18:03 -07001335#define dm_pci_io_to_virt(dev, addr, len, map_flags) \
Simon Glass4974a6f2016-03-06 19:27:53 -07001336 dm_pci_bus_to_virt((dev), (addr), PCI_REGION_IO, (len), (map_flags))
Simon Glass21d1fe72015-11-29 13:18:03 -07001337
1338/**
Simon Glass5c0bf642015-11-29 13:17:50 -07001339 * dm_pci_find_device() - find a device by vendor/device ID
1340 *
1341 * @vendor: Vendor ID
1342 * @device: Device ID
1343 * @index: 0 to find the first match, 1 for second, etc.
1344 * @devp: Returns pointer to the device, if found
1345 * @return 0 if found, -ve on error
1346 */
1347int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
1348 struct udevice **devp);
1349
1350/**
Simon Glassa0eb8352015-11-29 13:17:52 -07001351 * dm_pci_find_class() - find a device by class
1352 *
1353 * @find_class: 3-byte (24-bit) class value to find
1354 * @index: 0 to find the first match, 1 for second, etc.
1355 * @devp: Returns pointer to the device, if found
1356 * @return 0 if found, -ve on error
1357 */
1358int dm_pci_find_class(uint find_class, int index, struct udevice **devp);
1359
1360/**
Simon Glass36d0d3b2015-03-05 12:25:28 -07001361 * struct dm_pci_emul_ops - PCI device emulator operations
1362 */
1363struct dm_pci_emul_ops {
1364 /**
1365 * get_devfn(): Check which device and function this emulators
1366 *
1367 * @dev: device to check
1368 * @return the device and function this emulates, or -ve on error
1369 */
1370 int (*get_devfn)(struct udevice *dev);
1371 /**
1372 * read_config() - Read a PCI configuration value
1373 *
1374 * @dev: Emulated device to read from
1375 * @offset: Byte offset within the device's configuration space
1376 * @valuep: Place to put the returned value
1377 * @size: Access size
1378 * @return 0 if OK, -ve on error
1379 */
1380 int (*read_config)(struct udevice *dev, uint offset, ulong *valuep,
1381 enum pci_size_t size);
1382 /**
1383 * write_config() - Write a PCI configuration value
1384 *
1385 * @dev: Emulated device to write to
1386 * @offset: Byte offset within the device's configuration space
1387 * @value: Value to write
1388 * @size: Access size
1389 * @return 0 if OK, -ve on error
1390 */
1391 int (*write_config)(struct udevice *dev, uint offset, ulong value,
1392 enum pci_size_t size);
1393 /**
1394 * read_io() - Read a PCI I/O value
1395 *
1396 * @dev: Emulated device to read from
1397 * @addr: I/O address to read
1398 * @valuep: Place to put the returned value
1399 * @size: Access size
1400 * @return 0 if OK, -ENOENT if @addr is not mapped by this device,
1401 * other -ve value on error
1402 */
1403 int (*read_io)(struct udevice *dev, unsigned int addr, ulong *valuep,
1404 enum pci_size_t size);
1405 /**
1406 * write_io() - Write a PCI I/O value
1407 *
1408 * @dev: Emulated device to write from
1409 * @addr: I/O address to write
1410 * @value: Value to write
1411 * @size: Access size
1412 * @return 0 if OK, -ENOENT if @addr is not mapped by this device,
1413 * other -ve value on error
1414 */
1415 int (*write_io)(struct udevice *dev, unsigned int addr,
1416 ulong value, enum pci_size_t size);
1417 /**
1418 * map_physmem() - Map a device into sandbox memory
1419 *
1420 * @dev: Emulated device to map
1421 * @addr: Memory address, normally corresponding to a PCI BAR.
1422 * The device should have been configured to have a BAR
1423 * at this address.
1424 * @lenp: On entry, the size of the area to map, On exit it is
1425 * updated to the size actually mapped, which may be less
1426 * if the device has less space
1427 * @ptrp: Returns a pointer to the mapped address. The device's
1428 * space can be accessed as @lenp bytes starting here
1429 * @return 0 if OK, -ENOENT if @addr is not mapped by this device,
1430 * other -ve value on error
1431 */
1432 int (*map_physmem)(struct udevice *dev, phys_addr_t addr,
1433 unsigned long *lenp, void **ptrp);
1434 /**
1435 * unmap_physmem() - undo a memory mapping
1436 *
1437 * This must be called after map_physmem() to undo the mapping.
1438 * Some devices can use this to check what has been written into
1439 * their mapped memory and perform an operations they require on it.
1440 * In this way, map/unmap can be used as a sort of handshake between
1441 * the emulated device and its users.
1442 *
1443 * @dev: Emuated device to unmap
1444 * @vaddr: Mapped memory address, as passed to map_physmem()
1445 * @len: Size of area mapped, as returned by map_physmem()
1446 * @return 0 if OK, -ve on error
1447 */
1448 int (*unmap_physmem)(struct udevice *dev, const void *vaddr,
1449 unsigned long len);
1450};
1451
1452/* Get access to a PCI device emulator's operations */
1453#define pci_get_emul_ops(dev) ((struct dm_pci_emul_ops *)(dev)->driver->ops)
1454
1455/**
1456 * sandbox_pci_get_emul() - Get the emulation device for a PCI device
1457 *
1458 * Searches for a suitable emulator for the given PCI bus device
1459 *
1460 * @bus: PCI bus to search
1461 * @find_devfn: PCI device and function address (PCI_DEVFN())
Bin Meng43459982018-08-03 01:14:45 -07001462 * @containerp: Returns container device if found
Simon Glass36d0d3b2015-03-05 12:25:28 -07001463 * @emulp: Returns emulated device if found
1464 * @return 0 if found, -ENODEV if not found
1465 */
1466int sandbox_pci_get_emul(struct udevice *bus, pci_dev_t find_devfn,
Bin Meng43459982018-08-03 01:14:45 -07001467 struct udevice **containerp, struct udevice **emulp);
Simon Glass36d0d3b2015-03-05 12:25:28 -07001468
Simon Glassaba92962015-07-06 16:47:44 -06001469#endif /* CONFIG_DM_PCI */
1470
1471/**
1472 * PCI_DEVICE - macro used to describe a specific pci device
1473 * @vend: the 16 bit PCI Vendor ID
1474 * @dev: the 16 bit PCI Device ID
1475 *
1476 * This macro is used to create a struct pci_device_id that matches a
1477 * specific device. The subvendor and subdevice fields will be set to
1478 * PCI_ANY_ID.
1479 */
1480#define PCI_DEVICE(vend, dev) \
1481 .vendor = (vend), .device = (dev), \
1482 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
1483
1484/**
1485 * PCI_DEVICE_SUB - macro used to describe a specific pci device with subsystem
1486 * @vend: the 16 bit PCI Vendor ID
1487 * @dev: the 16 bit PCI Device ID
1488 * @subvend: the 16 bit PCI Subvendor ID
1489 * @subdev: the 16 bit PCI Subdevice ID
1490 *
1491 * This macro is used to create a struct pci_device_id that matches a
1492 * specific device with subsystem information.
1493 */
1494#define PCI_DEVICE_SUB(vend, dev, subvend, subdev) \
1495 .vendor = (vend), .device = (dev), \
1496 .subvendor = (subvend), .subdevice = (subdev)
1497
1498/**
1499 * PCI_DEVICE_CLASS - macro used to describe a specific pci device class
1500 * @dev_class: the class, subclass, prog-if triple for this device
1501 * @dev_class_mask: the class mask for this device
1502 *
1503 * This macro is used to create a struct pci_device_id that matches a
1504 * specific PCI class. The vendor, device, subvendor, and subdevice
1505 * fields will be set to PCI_ANY_ID.
1506 */
1507#define PCI_DEVICE_CLASS(dev_class, dev_class_mask) \
1508 .class = (dev_class), .class_mask = (dev_class_mask), \
1509 .vendor = PCI_ANY_ID, .device = PCI_ANY_ID, \
1510 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID
1511
1512/**
1513 * PCI_VDEVICE - macro used to describe a specific pci device in short form
1514 * @vend: the vendor name
1515 * @dev: the 16 bit PCI Device ID
1516 *
1517 * This macro is used to create a struct pci_device_id that matches a
1518 * specific PCI device. The subvendor, and subdevice fields will be set
1519 * to PCI_ANY_ID. The macro allows the next field to follow as the device
1520 * private data.
1521 */
1522
1523#define PCI_VDEVICE(vend, dev) \
1524 .vendor = PCI_VENDOR_ID_##vend, .device = (dev), \
1525 .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, 0, 0
1526
1527/**
1528 * struct pci_driver_entry - Matches a driver to its pci_device_id list
1529 * @driver: Driver to use
1530 * @match: List of match records for this driver, terminated by {}
1531 */
1532struct pci_driver_entry {
1533 struct driver *driver;
1534 const struct pci_device_id *match;
1535};
1536
1537#define U_BOOT_PCI_DEVICE(__name, __match) \
1538 ll_entry_declare(struct pci_driver_entry, __name, pci_driver_entry) = {\
1539 .driver = llsym(struct driver, __name, driver), \
1540 .match = __match, \
1541 }
Simon Glassff3e0772015-03-05 12:25:25 -07001542
Paul Burtonfa5cec02013-11-08 11:18:47 +00001543#endif /* __ASSEMBLY__ */
1544#endif /* _PCI_H */