blob: 5a6048c9260476300d8ab79ca1ef6cb8efa7cfb6 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
3 * Andreas Heppel <aheppel@sysgo.de>
4 *
5 * (C) Copyright 2002
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * Wolfgang Grandegger, DENX Software Engineering, wg@denx.de.
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +000010 */
11
12/*
13 * PCI routines
14 */
15
16#include <common.h>
Simon Glass399ed9a2014-04-10 20:01:30 -060017#include <bootretry.h>
Simon Glassdec3c012014-04-10 20:01:25 -060018#include <cli.h>
wdenkc6097192002-11-03 00:24:07 +000019#include <command.h>
wdenkc6097192002-11-03 00:24:07 +000020#include <asm/processor.h>
21#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000022#include <pci.h>
23
wdenkc6097192002-11-03 00:24:07 +000024/*
25 * Follows routines for the output of infos about devices on PCI bus.
26 */
27
28void pci_header_show(pci_dev_t dev);
29void pci_header_show_brief(pci_dev_t dev);
30
31/*
32 * Subroutine: pciinfo
33 *
34 * Description: Show information about devices on PCI bus.
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020035 * Depending on the define CONFIG_SYS_SHORT_PCI_LISTING
wdenkc6097192002-11-03 00:24:07 +000036 * the output will be more or less exhaustive.
37 *
38 * Inputs: bus_no the number of the bus to be scanned.
39 *
40 * Return: None
41 *
42 */
43void pciinfo(int BusNum, int ShortPCIListing)
44{
Thierry Reding68185b02014-11-12 18:26:48 -070045 struct pci_controller *hose = pci_bus_to_hose(BusNum);
wdenkc6097192002-11-03 00:24:07 +000046 int Device;
47 int Function;
48 unsigned char HeaderType;
49 unsigned short VendorID;
50 pci_dev_t dev;
51
Thierry Reding68185b02014-11-12 18:26:48 -070052 if (!hose)
53 return;
54
wdenkc6097192002-11-03 00:24:07 +000055 printf("Scanning PCI devices on bus %d\n", BusNum);
56
57 if (ShortPCIListing) {
58 printf("BusDevFun VendorId DeviceId Device Class Sub-Class\n");
59 printf("_____________________________________________________________\n");
60 }
61
62 for (Device = 0; Device < PCI_MAX_PCI_DEVICES; Device++) {
63 HeaderType = 0;
64 VendorID = 0;
65 for (Function = 0; Function < PCI_MAX_PCI_FUNCTIONS; Function++) {
66 /*
67 * If this is not a multi-function device, we skip the rest.
68 */
69 if (Function && !(HeaderType & 0x80))
70 break;
71
72 dev = PCI_BDF(BusNum, Device, Function);
73
74 pci_read_config_word(dev, PCI_VENDOR_ID, &VendorID);
75 if ((VendorID == 0xFFFF) || (VendorID == 0x0000))
76 continue;
77
wdenk452cfd62002-11-19 11:04:11 +000078 if (!Function) pci_read_config_byte(dev, PCI_HEADER_TYPE, &HeaderType);
wdenkc6097192002-11-03 00:24:07 +000079
80 if (ShortPCIListing)
81 {
82 printf("%02x.%02x.%02x ", BusNum, Device, Function);
83 pci_header_show_brief(dev);
84 }
85 else
86 {
87 printf("\nFound PCI device %02x.%02x.%02x:\n",
88 BusNum, Device, Function);
89 pci_header_show(dev);
90 }
91 }
92 }
93}
94
wdenkc6097192002-11-03 00:24:07 +000095
96/*
97 * Subroutine: pci_header_show_brief
98 *
99 * Description: Reads and prints the header of the
Wolfgang Denka1be4762008-05-20 16:00:29 +0200100 * specified PCI device in short form.
wdenkc6097192002-11-03 00:24:07 +0000101 *
102 * Inputs: dev Bus+Device+Function number
103 *
104 * Return: None
105 *
106 */
107void pci_header_show_brief(pci_dev_t dev)
108{
109 u16 vendor, device;
110 u8 class, subclass;
111
112 pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
113 pci_read_config_word(dev, PCI_DEVICE_ID, &device);
114 pci_read_config_byte(dev, PCI_CLASS_CODE, &class);
115 pci_read_config_byte(dev, PCI_CLASS_SUB_CODE, &subclass);
116
wdenkbf2f8c92003-05-22 22:52:13 +0000117 printf("0x%.4x 0x%.4x %-23s 0x%.2x\n",
wdenkc6097192002-11-03 00:24:07 +0000118 vendor, device,
Peter Tyser22ccb7f2010-10-29 17:59:27 -0500119 pci_class_str(class), subclass);
wdenkc6097192002-11-03 00:24:07 +0000120}
121
122/*
123 * Subroutine: PCI_Header_Show
124 *
125 * Description: Reads the header of the specified PCI device.
126 *
127 * Inputs: BusDevFunc Bus+Device+Function number
128 *
129 * Return: None
130 *
131 */
132void pci_header_show(pci_dev_t dev)
133{
134 u8 _byte, header_type;
135 u16 _word;
136 u32 _dword;
137
138#define PRINT(msg, type, reg) \
139 pci_read_config_##type(dev, reg, &_##type); \
140 printf(msg, _##type)
141
142#define PRINT2(msg, type, reg, func) \
143 pci_read_config_##type(dev, reg, &_##type); \
144 printf(msg, _##type, func(_##type))
145
146 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
147
148 PRINT (" vendor ID = 0x%.4x\n", word, PCI_VENDOR_ID);
149 PRINT (" device ID = 0x%.4x\n", word, PCI_DEVICE_ID);
150 PRINT (" command register = 0x%.4x\n", word, PCI_COMMAND);
151 PRINT (" status register = 0x%.4x\n", word, PCI_STATUS);
152 PRINT (" revision ID = 0x%.2x\n", byte, PCI_REVISION_ID);
153 PRINT2(" class code = 0x%.2x (%s)\n", byte, PCI_CLASS_CODE,
Peter Tyser22ccb7f2010-10-29 17:59:27 -0500154 pci_class_str);
wdenkc6097192002-11-03 00:24:07 +0000155 PRINT (" sub class code = 0x%.2x\n", byte, PCI_CLASS_SUB_CODE);
156 PRINT (" programming interface = 0x%.2x\n", byte, PCI_CLASS_PROG);
157 PRINT (" cache line = 0x%.2x\n", byte, PCI_CACHE_LINE_SIZE);
158 PRINT (" latency time = 0x%.2x\n", byte, PCI_LATENCY_TIMER);
159 PRINT (" header type = 0x%.2x\n", byte, PCI_HEADER_TYPE);
160 PRINT (" BIST = 0x%.2x\n", byte, PCI_BIST);
161 PRINT (" base address 0 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_0);
wdenkc6097192002-11-03 00:24:07 +0000162
wdenkbb444c92002-12-07 00:20:59 +0000163 switch (header_type & 0x03) {
164 case PCI_HEADER_TYPE_NORMAL: /* "normal" PCI device */
165 PRINT (" base address 1 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_1);
166 PRINT (" base address 2 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_2);
167 PRINT (" base address 3 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_3);
168 PRINT (" base address 4 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_4);
169 PRINT (" base address 5 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_5);
170 PRINT (" cardBus CIS pointer = 0x%.8x\n", dword, PCI_CARDBUS_CIS);
171 PRINT (" sub system vendor ID = 0x%.4x\n", word, PCI_SUBSYSTEM_VENDOR_ID);
172 PRINT (" sub system ID = 0x%.4x\n", word, PCI_SUBSYSTEM_ID);
173 PRINT (" expansion ROM base address = 0x%.8x\n", dword, PCI_ROM_ADDRESS);
174 PRINT (" interrupt line = 0x%.2x\n", byte, PCI_INTERRUPT_LINE);
175 PRINT (" interrupt pin = 0x%.2x\n", byte, PCI_INTERRUPT_PIN);
176 PRINT (" min Grant = 0x%.2x\n", byte, PCI_MIN_GNT);
177 PRINT (" max Latency = 0x%.2x\n", byte, PCI_MAX_LAT);
178 break;
wdenk57b2d802003-06-27 21:31:46 +0000179
wdenkbb444c92002-12-07 00:20:59 +0000180 case PCI_HEADER_TYPE_BRIDGE: /* PCI-to-PCI bridge */
181
182 PRINT (" base address 1 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_1);
wdenkc6097192002-11-03 00:24:07 +0000183 PRINT (" primary bus number = 0x%.2x\n", byte, PCI_PRIMARY_BUS);
184 PRINT (" secondary bus number = 0x%.2x\n", byte, PCI_SECONDARY_BUS);
185 PRINT (" subordinate bus number = 0x%.2x\n", byte, PCI_SUBORDINATE_BUS);
186 PRINT (" secondary latency timer = 0x%.2x\n", byte, PCI_SEC_LATENCY_TIMER);
187 PRINT (" IO base = 0x%.2x\n", byte, PCI_IO_BASE);
188 PRINT (" IO limit = 0x%.2x\n", byte, PCI_IO_LIMIT);
189 PRINT (" secondary status = 0x%.4x\n", word, PCI_SEC_STATUS);
190 PRINT (" memory base = 0x%.4x\n", word, PCI_MEMORY_BASE);
191 PRINT (" memory limit = 0x%.4x\n", word, PCI_MEMORY_LIMIT);
192 PRINT (" prefetch memory base = 0x%.4x\n", word, PCI_PREF_MEMORY_BASE);
193 PRINT (" prefetch memory limit = 0x%.4x\n", word, PCI_PREF_MEMORY_LIMIT);
194 PRINT (" prefetch memory base upper = 0x%.8x\n", dword, PCI_PREF_BASE_UPPER32);
195 PRINT (" prefetch memory limit upper = 0x%.8x\n", dword, PCI_PREF_LIMIT_UPPER32);
196 PRINT (" IO base upper 16 bits = 0x%.4x\n", word, PCI_IO_BASE_UPPER16);
197 PRINT (" IO limit upper 16 bits = 0x%.4x\n", word, PCI_IO_LIMIT_UPPER16);
198 PRINT (" expansion ROM base address = 0x%.8x\n", dword, PCI_ROM_ADDRESS1);
199 PRINT (" interrupt line = 0x%.2x\n", byte, PCI_INTERRUPT_LINE);
200 PRINT (" interrupt pin = 0x%.2x\n", byte, PCI_INTERRUPT_PIN);
201 PRINT (" bridge control = 0x%.4x\n", word, PCI_BRIDGE_CONTROL);
wdenkbb444c92002-12-07 00:20:59 +0000202 break;
203
204 case PCI_HEADER_TYPE_CARDBUS: /* PCI-to-CardBus bridge */
205
206 PRINT (" capabilities = 0x%.2x\n", byte, PCI_CB_CAPABILITY_LIST);
207 PRINT (" secondary status = 0x%.4x\n", word, PCI_CB_SEC_STATUS);
208 PRINT (" primary bus number = 0x%.2x\n", byte, PCI_CB_PRIMARY_BUS);
209 PRINT (" CardBus number = 0x%.2x\n", byte, PCI_CB_CARD_BUS);
210 PRINT (" subordinate bus number = 0x%.2x\n", byte, PCI_CB_SUBORDINATE_BUS);
wdenk57b2d802003-06-27 21:31:46 +0000211 PRINT (" CardBus latency timer = 0x%.2x\n", byte, PCI_CB_LATENCY_TIMER);
wdenkbb444c92002-12-07 00:20:59 +0000212 PRINT (" CardBus memory base 0 = 0x%.8x\n", dword, PCI_CB_MEMORY_BASE_0);
213 PRINT (" CardBus memory limit 0 = 0x%.8x\n", dword, PCI_CB_MEMORY_LIMIT_0);
214 PRINT (" CardBus memory base 1 = 0x%.8x\n", dword, PCI_CB_MEMORY_BASE_1);
215 PRINT (" CardBus memory limit 1 = 0x%.8x\n", dword, PCI_CB_MEMORY_LIMIT_1);
216 PRINT (" CardBus IO base 0 = 0x%.4x\n", word, PCI_CB_IO_BASE_0);
217 PRINT (" CardBus IO base high 0 = 0x%.4x\n", word, PCI_CB_IO_BASE_0_HI);
218 PRINT (" CardBus IO limit 0 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_0);
219 PRINT (" CardBus IO limit high 0 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_0_HI);
220 PRINT (" CardBus IO base 1 = 0x%.4x\n", word, PCI_CB_IO_BASE_1);
221 PRINT (" CardBus IO base high 1 = 0x%.4x\n", word, PCI_CB_IO_BASE_1_HI);
222 PRINT (" CardBus IO limit 1 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_1);
223 PRINT (" CardBus IO limit high 1 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_1_HI);
224 PRINT (" interrupt line = 0x%.2x\n", byte, PCI_INTERRUPT_LINE);
225 PRINT (" interrupt pin = 0x%.2x\n", byte, PCI_INTERRUPT_PIN);
226 PRINT (" bridge control = 0x%.4x\n", word, PCI_CB_BRIDGE_CONTROL);
227 PRINT (" subvendor ID = 0x%.4x\n", word, PCI_CB_SUBSYSTEM_VENDOR_ID);
228 PRINT (" subdevice ID = 0x%.4x\n", word, PCI_CB_SUBSYSTEM_ID);
229 PRINT (" PC Card 16bit base address = 0x%.8x\n", dword, PCI_CB_LEGACY_MODE_BASE);
230 break;
wdenk57b2d802003-06-27 21:31:46 +0000231
wdenkbb444c92002-12-07 00:20:59 +0000232 default:
233 printf("unknown header\n");
wdenk57b2d802003-06-27 21:31:46 +0000234 break;
wdenkc6097192002-11-03 00:24:07 +0000235 }
236
237#undef PRINT
238#undef PRINT2
239}
240
241/* Convert the "bus.device.function" identifier into a number.
242 */
243static pci_dev_t get_pci_dev(char* name)
244{
245 char cnum[12];
246 int len, i, iold, n;
247 int bdfs[3] = {0,0,0};
248
249 len = strlen(name);
250 if (len > 8)
251 return -1;
252 for (i = 0, iold = 0, n = 0; i < len; i++) {
253 if (name[i] == '.') {
254 memcpy(cnum, &name[iold], i - iold);
255 cnum[i - iold] = '\0';
256 bdfs[n++] = simple_strtoul(cnum, NULL, 16);
257 iold = i + 1;
258 }
259 }
260 strcpy(cnum, &name[iold]);
261 if (n == 0)
262 n = 1;
263 bdfs[n] = simple_strtoul(cnum, NULL, 16);
264 return PCI_BDF(bdfs[0], bdfs[1], bdfs[2]);
265}
266
267static int pci_cfg_display(pci_dev_t bdf, ulong addr, ulong size, ulong length)
268{
269#define DISP_LINE_LEN 16
270 ulong i, nbytes, linebytes;
271 int rc = 0;
272
273 if (length == 0)
274 length = 0x40 / size; /* Standard PCI configuration space */
275
276 /* Print the lines.
277 * once, and all accesses are with the specified bus width.
278 */
279 nbytes = length * size;
280 do {
281 uint val4;
282 ushort val2;
283 u_char val1;
284
285 printf("%08lx:", addr);
286 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
287 for (i=0; i<linebytes; i+= size) {
288 if (size == 4) {
289 pci_read_config_dword(bdf, addr, &val4);
290 printf(" %08x", val4);
291 } else if (size == 2) {
292 pci_read_config_word(bdf, addr, &val2);
293 printf(" %04x", val2);
294 } else {
295 pci_read_config_byte(bdf, addr, &val1);
296 printf(" %02x", val1);
297 }
298 addr += size;
299 }
300 printf("\n");
301 nbytes -= linebytes;
302 if (ctrlc()) {
303 rc = 1;
304 break;
305 }
306 } while (nbytes > 0);
307
308 return (rc);
309}
310
311static int pci_cfg_write (pci_dev_t bdf, ulong addr, ulong size, ulong value)
312{
313 if (size == 4) {
314 pci_write_config_dword(bdf, addr, value);
315 }
316 else if (size == 2) {
317 ushort val = value & 0xffff;
318 pci_write_config_word(bdf, addr, val);
319 }
320 else {
321 u_char val = value & 0xff;
322 pci_write_config_byte(bdf, addr, val);
323 }
324 return 0;
325}
326
327static int
328pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag)
329{
330 ulong i;
331 int nbytes;
wdenkc6097192002-11-03 00:24:07 +0000332 uint val4;
333 ushort val2;
334 u_char val1;
335
336 /* Print the address, followed by value. Then accept input for
337 * the next value. A non-converted value exits.
338 */
339 do {
340 printf("%08lx:", addr);
341 if (size == 4) {
342 pci_read_config_dword(bdf, addr, &val4);
343 printf(" %08x", val4);
344 }
345 else if (size == 2) {
346 pci_read_config_word(bdf, addr, &val2);
347 printf(" %04x", val2);
348 }
349 else {
350 pci_read_config_byte(bdf, addr, &val1);
351 printf(" %02x", val1);
352 }
353
Simon Glassbe6aafc2014-04-10 20:01:27 -0600354 nbytes = cli_readline(" ? ");
wdenkc6097192002-11-03 00:24:07 +0000355 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
356 /* <CR> pressed as only input, don't modify current
357 * location and move to next. "-" pressed will go back.
358 */
359 if (incrflag)
360 addr += nbytes ? -size : size;
361 nbytes = 1;
Simon Glass09007c42014-04-10 20:01:31 -0600362 /* good enough to not time out */
363 bootretry_reset_cmd_timeout();
wdenkc6097192002-11-03 00:24:07 +0000364 }
365#ifdef CONFIG_BOOT_RETRY_TIME
366 else if (nbytes == -2) {
367 break; /* timed out, exit the command */
368 }
369#endif
370 else {
371 char *endp;
372 i = simple_strtoul(console_buffer, &endp, 16);
373 nbytes = endp - console_buffer;
374 if (nbytes) {
wdenkc6097192002-11-03 00:24:07 +0000375 /* good enough to not time out
376 */
Simon Glass09007c42014-04-10 20:01:31 -0600377 bootretry_reset_cmd_timeout();
wdenkc6097192002-11-03 00:24:07 +0000378 pci_cfg_write (bdf, addr, size, i);
379 if (incrflag)
380 addr += size;
381 }
382 }
383 } while (nbytes);
384
385 return 0;
386}
387
388/* PCI Configuration Space access commands
389 *
390 * Syntax:
391 * pci display[.b, .w, .l] bus.device.function} [addr] [len]
392 * pci next[.b, .w, .l] bus.device.function [addr]
393 * pci modify[.b, .w, .l] bus.device.function [addr]
394 * pci write[.b, .w, .l] bus.device.function addr value
395 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000396static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000397{
398 ulong addr = 0, value = 0, size = 0;
399 pci_dev_t bdf = 0;
400 char cmd = 's';
401
402 if (argc > 1)
403 cmd = argv[1][0];
404
405 switch (cmd) {
406 case 'd': /* display */
407 case 'n': /* next */
408 case 'm': /* modify */
409 case 'w': /* write */
410 /* Check for a size specification. */
411 size = cmd_get_data_size(argv[1], 4);
412 if (argc > 3)
413 addr = simple_strtoul(argv[3], NULL, 16);
414 if (argc > 4)
415 value = simple_strtoul(argv[4], NULL, 16);
416 case 'h': /* header */
417 if (argc < 3)
418 goto usage;
419 if ((bdf = get_pci_dev(argv[2])) == -1)
420 return 1;
421 break;
John Schmoller60e877f2010-10-22 00:20:23 -0500422#ifdef CONFIG_CMD_PCI_ENUM
423 case 'e':
424 break;
425#endif
wdenkc6097192002-11-03 00:24:07 +0000426 default: /* scan bus */
427 value = 1; /* short listing */
428 bdf = 0; /* bus number */
429 if (argc > 1) {
430 if (argv[argc-1][0] == 'l') {
431 value = 0;
432 argc--;
433 }
434 if (argc > 1)
435 bdf = simple_strtoul(argv[1], NULL, 16);
436 }
437 pciinfo(bdf, value);
438 return 0;
439 }
440
441 switch (argv[1][0]) {
442 case 'h': /* header */
443 pci_header_show(bdf);
444 return 0;
445 case 'd': /* display */
446 return pci_cfg_display(bdf, addr, size, value);
John Schmoller60e877f2010-10-22 00:20:23 -0500447#ifdef CONFIG_CMD_PCI_ENUM
448 case 'e':
449 pci_init();
450 return 0;
451#endif
wdenkc6097192002-11-03 00:24:07 +0000452 case 'n': /* next */
453 if (argc < 4)
454 goto usage;
455 return pci_cfg_modify(bdf, addr, size, value, 0);
456 case 'm': /* modify */
457 if (argc < 4)
458 goto usage;
459 return pci_cfg_modify(bdf, addr, size, value, 1);
460 case 'w': /* write */
461 if (argc < 5)
462 goto usage;
463 return pci_cfg_write(bdf, addr, size, value);
464 }
465
466 return 1;
467 usage:
Simon Glassa06dfc72011-12-10 08:44:01 +0000468 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +0000469}
470
wdenk57b2d802003-06-27 21:31:46 +0000471/***************************************************/
472
Kim Phillipsdc00a682012-10-29 13:34:31 +0000473#ifdef CONFIG_SYS_LONGHELP
474static char pci_help_text[] =
wdenk57b2d802003-06-27 21:31:46 +0000475 "[bus] [long]\n"
476 " - short or long list of PCI devices on bus 'bus'\n"
John Schmoller60e877f2010-10-22 00:20:23 -0500477#ifdef CONFIG_CMD_PCI_ENUM
478 "pci enum\n"
479 " - re-enumerate PCI buses\n"
480#endif
wdenk57b2d802003-06-27 21:31:46 +0000481 "pci header b.d.f\n"
482 " - show header of PCI device 'bus.device.function'\n"
483 "pci display[.b, .w, .l] b.d.f [address] [# of objects]\n"
484 " - display PCI configuration space (CFG)\n"
485 "pci next[.b, .w, .l] b.d.f address\n"
486 " - modify, read and keep CFG address\n"
487 "pci modify[.b, .w, .l] b.d.f address\n"
488 " - modify, auto increment CFG address\n"
489 "pci write[.b, .w, .l] b.d.f address value\n"
Kim Phillipsdc00a682012-10-29 13:34:31 +0000490 " - write to CFG address";
491#endif
492
493U_BOOT_CMD(
494 pci, 5, 1, do_pci,
495 "list and access PCI Configuration Space", pci_help_text
wdenk57b2d802003-06-27 21:31:46 +0000496);