2 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
3 * Andreas Heppel <aheppel@sysgo.de>
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * Wolfgang Grandegger, DENX Software Engineering, wg@denx.de.
9 * SPDX-License-Identifier: GPL-2.0+
18 #include <asm/processor.h>
23 * Follows routines for the output of infos about devices on PCI bus.
26 void pci_header_show(pci_dev_t dev);
27 void pci_header_show_brief(pci_dev_t dev);
32 * Description: Show information about devices on PCI bus.
33 * Depending on the define CONFIG_SYS_SHORT_PCI_LISTING
34 * the output will be more or less exhaustive.
36 * Inputs: bus_no the number of the bus to be scanned.
41 void pciinfo(int BusNum, int ShortPCIListing)
45 unsigned char HeaderType;
46 unsigned short VendorID;
49 printf("Scanning PCI devices on bus %d\n", BusNum);
51 if (ShortPCIListing) {
52 printf("BusDevFun VendorId DeviceId Device Class Sub-Class\n");
53 printf("_____________________________________________________________\n");
56 for (Device = 0; Device < PCI_MAX_PCI_DEVICES; Device++) {
59 for (Function = 0; Function < PCI_MAX_PCI_FUNCTIONS; Function++) {
61 * If this is not a multi-function device, we skip the rest.
63 if (Function && !(HeaderType & 0x80))
66 dev = PCI_BDF(BusNum, Device, Function);
68 pci_read_config_word(dev, PCI_VENDOR_ID, &VendorID);
69 if ((VendorID == 0xFFFF) || (VendorID == 0x0000))
72 if (!Function) pci_read_config_byte(dev, PCI_HEADER_TYPE, &HeaderType);
76 printf("%02x.%02x.%02x ", BusNum, Device, Function);
77 pci_header_show_brief(dev);
81 printf("\nFound PCI device %02x.%02x.%02x:\n",
82 BusNum, Device, Function);
91 * Subroutine: pci_header_show_brief
93 * Description: Reads and prints the header of the
94 * specified PCI device in short form.
96 * Inputs: dev Bus+Device+Function number
101 void pci_header_show_brief(pci_dev_t dev)
106 pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
107 pci_read_config_word(dev, PCI_DEVICE_ID, &device);
108 pci_read_config_byte(dev, PCI_CLASS_CODE, &class);
109 pci_read_config_byte(dev, PCI_CLASS_SUB_CODE, &subclass);
111 printf("0x%.4x 0x%.4x %-23s 0x%.2x\n",
113 pci_class_str(class), subclass);
117 * Subroutine: PCI_Header_Show
119 * Description: Reads the header of the specified PCI device.
121 * Inputs: BusDevFunc Bus+Device+Function number
126 void pci_header_show(pci_dev_t dev)
128 u8 _byte, header_type;
132 #define PRINT(msg, type, reg) \
133 pci_read_config_##type(dev, reg, &_##type); \
136 #define PRINT2(msg, type, reg, func) \
137 pci_read_config_##type(dev, reg, &_##type); \
138 printf(msg, _##type, func(_##type))
140 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
142 PRINT (" vendor ID = 0x%.4x\n", word, PCI_VENDOR_ID);
143 PRINT (" device ID = 0x%.4x\n", word, PCI_DEVICE_ID);
144 PRINT (" command register = 0x%.4x\n", word, PCI_COMMAND);
145 PRINT (" status register = 0x%.4x\n", word, PCI_STATUS);
146 PRINT (" revision ID = 0x%.2x\n", byte, PCI_REVISION_ID);
147 PRINT2(" class code = 0x%.2x (%s)\n", byte, PCI_CLASS_CODE,
149 PRINT (" sub class code = 0x%.2x\n", byte, PCI_CLASS_SUB_CODE);
150 PRINT (" programming interface = 0x%.2x\n", byte, PCI_CLASS_PROG);
151 PRINT (" cache line = 0x%.2x\n", byte, PCI_CACHE_LINE_SIZE);
152 PRINT (" latency time = 0x%.2x\n", byte, PCI_LATENCY_TIMER);
153 PRINT (" header type = 0x%.2x\n", byte, PCI_HEADER_TYPE);
154 PRINT (" BIST = 0x%.2x\n", byte, PCI_BIST);
155 PRINT (" base address 0 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_0);
157 switch (header_type & 0x03) {
158 case PCI_HEADER_TYPE_NORMAL: /* "normal" PCI device */
159 PRINT (" base address 1 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_1);
160 PRINT (" base address 2 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_2);
161 PRINT (" base address 3 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_3);
162 PRINT (" base address 4 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_4);
163 PRINT (" base address 5 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_5);
164 PRINT (" cardBus CIS pointer = 0x%.8x\n", dword, PCI_CARDBUS_CIS);
165 PRINT (" sub system vendor ID = 0x%.4x\n", word, PCI_SUBSYSTEM_VENDOR_ID);
166 PRINT (" sub system ID = 0x%.4x\n", word, PCI_SUBSYSTEM_ID);
167 PRINT (" expansion ROM base address = 0x%.8x\n", dword, PCI_ROM_ADDRESS);
168 PRINT (" interrupt line = 0x%.2x\n", byte, PCI_INTERRUPT_LINE);
169 PRINT (" interrupt pin = 0x%.2x\n", byte, PCI_INTERRUPT_PIN);
170 PRINT (" min Grant = 0x%.2x\n", byte, PCI_MIN_GNT);
171 PRINT (" max Latency = 0x%.2x\n", byte, PCI_MAX_LAT);
174 case PCI_HEADER_TYPE_BRIDGE: /* PCI-to-PCI bridge */
176 PRINT (" base address 1 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_1);
177 PRINT (" primary bus number = 0x%.2x\n", byte, PCI_PRIMARY_BUS);
178 PRINT (" secondary bus number = 0x%.2x\n", byte, PCI_SECONDARY_BUS);
179 PRINT (" subordinate bus number = 0x%.2x\n", byte, PCI_SUBORDINATE_BUS);
180 PRINT (" secondary latency timer = 0x%.2x\n", byte, PCI_SEC_LATENCY_TIMER);
181 PRINT (" IO base = 0x%.2x\n", byte, PCI_IO_BASE);
182 PRINT (" IO limit = 0x%.2x\n", byte, PCI_IO_LIMIT);
183 PRINT (" secondary status = 0x%.4x\n", word, PCI_SEC_STATUS);
184 PRINT (" memory base = 0x%.4x\n", word, PCI_MEMORY_BASE);
185 PRINT (" memory limit = 0x%.4x\n", word, PCI_MEMORY_LIMIT);
186 PRINT (" prefetch memory base = 0x%.4x\n", word, PCI_PREF_MEMORY_BASE);
187 PRINT (" prefetch memory limit = 0x%.4x\n", word, PCI_PREF_MEMORY_LIMIT);
188 PRINT (" prefetch memory base upper = 0x%.8x\n", dword, PCI_PREF_BASE_UPPER32);
189 PRINT (" prefetch memory limit upper = 0x%.8x\n", dword, PCI_PREF_LIMIT_UPPER32);
190 PRINT (" IO base upper 16 bits = 0x%.4x\n", word, PCI_IO_BASE_UPPER16);
191 PRINT (" IO limit upper 16 bits = 0x%.4x\n", word, PCI_IO_LIMIT_UPPER16);
192 PRINT (" expansion ROM base address = 0x%.8x\n", dword, PCI_ROM_ADDRESS1);
193 PRINT (" interrupt line = 0x%.2x\n", byte, PCI_INTERRUPT_LINE);
194 PRINT (" interrupt pin = 0x%.2x\n", byte, PCI_INTERRUPT_PIN);
195 PRINT (" bridge control = 0x%.4x\n", word, PCI_BRIDGE_CONTROL);
198 case PCI_HEADER_TYPE_CARDBUS: /* PCI-to-CardBus bridge */
200 PRINT (" capabilities = 0x%.2x\n", byte, PCI_CB_CAPABILITY_LIST);
201 PRINT (" secondary status = 0x%.4x\n", word, PCI_CB_SEC_STATUS);
202 PRINT (" primary bus number = 0x%.2x\n", byte, PCI_CB_PRIMARY_BUS);
203 PRINT (" CardBus number = 0x%.2x\n", byte, PCI_CB_CARD_BUS);
204 PRINT (" subordinate bus number = 0x%.2x\n", byte, PCI_CB_SUBORDINATE_BUS);
205 PRINT (" CardBus latency timer = 0x%.2x\n", byte, PCI_CB_LATENCY_TIMER);
206 PRINT (" CardBus memory base 0 = 0x%.8x\n", dword, PCI_CB_MEMORY_BASE_0);
207 PRINT (" CardBus memory limit 0 = 0x%.8x\n", dword, PCI_CB_MEMORY_LIMIT_0);
208 PRINT (" CardBus memory base 1 = 0x%.8x\n", dword, PCI_CB_MEMORY_BASE_1);
209 PRINT (" CardBus memory limit 1 = 0x%.8x\n", dword, PCI_CB_MEMORY_LIMIT_1);
210 PRINT (" CardBus IO base 0 = 0x%.4x\n", word, PCI_CB_IO_BASE_0);
211 PRINT (" CardBus IO base high 0 = 0x%.4x\n", word, PCI_CB_IO_BASE_0_HI);
212 PRINT (" CardBus IO limit 0 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_0);
213 PRINT (" CardBus IO limit high 0 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_0_HI);
214 PRINT (" CardBus IO base 1 = 0x%.4x\n", word, PCI_CB_IO_BASE_1);
215 PRINT (" CardBus IO base high 1 = 0x%.4x\n", word, PCI_CB_IO_BASE_1_HI);
216 PRINT (" CardBus IO limit 1 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_1);
217 PRINT (" CardBus IO limit high 1 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_1_HI);
218 PRINT (" interrupt line = 0x%.2x\n", byte, PCI_INTERRUPT_LINE);
219 PRINT (" interrupt pin = 0x%.2x\n", byte, PCI_INTERRUPT_PIN);
220 PRINT (" bridge control = 0x%.4x\n", word, PCI_CB_BRIDGE_CONTROL);
221 PRINT (" subvendor ID = 0x%.4x\n", word, PCI_CB_SUBSYSTEM_VENDOR_ID);
222 PRINT (" subdevice ID = 0x%.4x\n", word, PCI_CB_SUBSYSTEM_ID);
223 PRINT (" PC Card 16bit base address = 0x%.8x\n", dword, PCI_CB_LEGACY_MODE_BASE);
227 printf("unknown header\n");
235 /* Convert the "bus.device.function" identifier into a number.
237 static pci_dev_t get_pci_dev(char* name)
241 int bdfs[3] = {0,0,0};
246 for (i = 0, iold = 0, n = 0; i < len; i++) {
247 if (name[i] == '.') {
248 memcpy(cnum, &name[iold], i - iold);
249 cnum[i - iold] = '\0';
250 bdfs[n++] = simple_strtoul(cnum, NULL, 16);
254 strcpy(cnum, &name[iold]);
257 bdfs[n] = simple_strtoul(cnum, NULL, 16);
258 return PCI_BDF(bdfs[0], bdfs[1], bdfs[2]);
261 static int pci_cfg_display(pci_dev_t bdf, ulong addr, ulong size, ulong length)
263 #define DISP_LINE_LEN 16
264 ulong i, nbytes, linebytes;
268 length = 0x40 / size; /* Standard PCI configuration space */
271 * once, and all accesses are with the specified bus width.
273 nbytes = length * size;
279 printf("%08lx:", addr);
280 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
281 for (i=0; i<linebytes; i+= size) {
283 pci_read_config_dword(bdf, addr, &val4);
284 printf(" %08x", val4);
285 } else if (size == 2) {
286 pci_read_config_word(bdf, addr, &val2);
287 printf(" %04x", val2);
289 pci_read_config_byte(bdf, addr, &val1);
290 printf(" %02x", val1);
300 } while (nbytes > 0);
305 static int pci_cfg_write (pci_dev_t bdf, ulong addr, ulong size, ulong value)
308 pci_write_config_dword(bdf, addr, value);
310 else if (size == 2) {
311 ushort val = value & 0xffff;
312 pci_write_config_word(bdf, addr, val);
315 u_char val = value & 0xff;
316 pci_write_config_byte(bdf, addr, val);
322 pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag)
330 /* Print the address, followed by value. Then accept input for
331 * the next value. A non-converted value exits.
334 printf("%08lx:", addr);
336 pci_read_config_dword(bdf, addr, &val4);
337 printf(" %08x", val4);
339 else if (size == 2) {
340 pci_read_config_word(bdf, addr, &val2);
341 printf(" %04x", val2);
344 pci_read_config_byte(bdf, addr, &val1);
345 printf(" %02x", val1);
348 nbytes = readline (" ? ");
349 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
350 /* <CR> pressed as only input, don't modify current
351 * location and move to next. "-" pressed will go back.
354 addr += nbytes ? -size : size;
356 #ifdef CONFIG_BOOT_RETRY_TIME
357 reset_cmd_timeout(); /* good enough to not time out */
360 #ifdef CONFIG_BOOT_RETRY_TIME
361 else if (nbytes == -2) {
362 break; /* timed out, exit the command */
367 i = simple_strtoul(console_buffer, &endp, 16);
368 nbytes = endp - console_buffer;
370 #ifdef CONFIG_BOOT_RETRY_TIME
371 /* good enough to not time out
375 pci_cfg_write (bdf, addr, size, i);
385 /* PCI Configuration Space access commands
388 * pci display[.b, .w, .l] bus.device.function} [addr] [len]
389 * pci next[.b, .w, .l] bus.device.function [addr]
390 * pci modify[.b, .w, .l] bus.device.function [addr]
391 * pci write[.b, .w, .l] bus.device.function addr value
393 static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
395 ulong addr = 0, value = 0, size = 0;
403 case 'd': /* display */
405 case 'm': /* modify */
406 case 'w': /* write */
407 /* Check for a size specification. */
408 size = cmd_get_data_size(argv[1], 4);
410 addr = simple_strtoul(argv[3], NULL, 16);
412 value = simple_strtoul(argv[4], NULL, 16);
413 case 'h': /* header */
416 if ((bdf = get_pci_dev(argv[2])) == -1)
419 #ifdef CONFIG_CMD_PCI_ENUM
423 default: /* scan bus */
424 value = 1; /* short listing */
425 bdf = 0; /* bus number */
427 if (argv[argc-1][0] == 'l') {
432 bdf = simple_strtoul(argv[1], NULL, 16);
438 switch (argv[1][0]) {
439 case 'h': /* header */
440 pci_header_show(bdf);
442 case 'd': /* display */
443 return pci_cfg_display(bdf, addr, size, value);
444 #ifdef CONFIG_CMD_PCI_ENUM
452 return pci_cfg_modify(bdf, addr, size, value, 0);
453 case 'm': /* modify */
456 return pci_cfg_modify(bdf, addr, size, value, 1);
457 case 'w': /* write */
460 return pci_cfg_write(bdf, addr, size, value);
465 return CMD_RET_USAGE;
468 /***************************************************/
470 #ifdef CONFIG_SYS_LONGHELP
471 static char pci_help_text[] =
473 " - short or long list of PCI devices on bus 'bus'\n"
474 #ifdef CONFIG_CMD_PCI_ENUM
476 " - re-enumerate PCI buses\n"
479 " - show header of PCI device 'bus.device.function'\n"
480 "pci display[.b, .w, .l] b.d.f [address] [# of objects]\n"
481 " - display PCI configuration space (CFG)\n"
482 "pci next[.b, .w, .l] b.d.f address\n"
483 " - modify, read and keep CFG address\n"
484 "pci modify[.b, .w, .l] b.d.f address\n"
485 " - modify, auto increment CFG address\n"
486 "pci write[.b, .w, .l] b.d.f address value\n"
487 " - write to CFG address";
492 "list and access PCI Configuration Space", pci_help_text