]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_pci.c
pci: Abort early if bus does not exist
[karo-tx-uboot.git] / common / cmd_pci.c
1 /*
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  *
9  * SPDX-License-Identifier:     GPL-2.0+
10  */
11
12 /*
13  * PCI routines
14  */
15
16 #include <common.h>
17 #include <bootretry.h>
18 #include <cli.h>
19 #include <command.h>
20 #include <asm/processor.h>
21 #include <asm/io.h>
22 #include <pci.h>
23
24 /*
25  * Follows routines for the output of infos about devices on PCI bus.
26  */
27
28 void pci_header_show(pci_dev_t dev);
29 void pci_header_show_brief(pci_dev_t dev);
30
31 /*
32  * Subroutine:  pciinfo
33  *
34  * Description: Show information about devices on PCI bus.
35  *                              Depending on the define CONFIG_SYS_SHORT_PCI_LISTING
36  *                              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  */
43 void pciinfo(int BusNum, int ShortPCIListing)
44 {
45         struct pci_controller *hose = pci_bus_to_hose(BusNum);
46         int Device;
47         int Function;
48         unsigned char HeaderType;
49         unsigned short VendorID;
50         pci_dev_t dev;
51
52         if (!hose)
53                 return;
54
55         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
78                         if (!Function) pci_read_config_byte(dev, PCI_HEADER_TYPE, &HeaderType);
79
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
95
96 /*
97  * Subroutine:  pci_header_show_brief
98  *
99  * Description: Reads and prints the header of the
100  *              specified PCI device in short form.
101  *
102  * Inputs:      dev      Bus+Device+Function number
103  *
104  * Return:      None
105  *
106  */
107 void 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
117         printf("0x%.4x     0x%.4x     %-23s 0x%.2x\n",
118                vendor, device,
119                pci_class_str(class), subclass);
120 }
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  */
132 void 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,
154                                                                 pci_class_str);
155         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);
162
163         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;
179
180         case PCI_HEADER_TYPE_BRIDGE:    /* PCI-to-PCI bridge */
181
182                 PRINT ("  base address 1 =              0x%.8x\n", dword, PCI_BASE_ADDRESS_1);
183                 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);
202                 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);
211                 PRINT ("  CardBus latency timer =       0x%.2x\n", byte, PCI_CB_LATENCY_TIMER);
212                 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;
231
232         default:
233                 printf("unknown header\n");
234                 break;
235     }
236
237 #undef PRINT
238 #undef PRINT2
239 }
240
241 /* Convert the "bus.device.function" identifier into a number.
242  */
243 static 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
267 static 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
311 static 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
327 static int
328 pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag)
329 {
330         ulong   i;
331         int     nbytes;
332         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
354                 nbytes = cli_readline(" ? ");
355                 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;
362                         /* good enough to not time out */
363                         bootretry_reset_cmd_timeout();
364                 }
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) {
375                                 /* good enough to not time out
376                                  */
377                                 bootretry_reset_cmd_timeout();
378                                 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  */
396 static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
397 {
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;
422 #ifdef CONFIG_CMD_PCI_ENUM
423         case 'e':
424                 break;
425 #endif
426         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);
447 #ifdef CONFIG_CMD_PCI_ENUM
448         case 'e':
449                 pci_init();
450                 return 0;
451 #endif
452         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:
468         return CMD_RET_USAGE;
469 }
470
471 /***************************************************/
472
473 #ifdef CONFIG_SYS_LONGHELP
474 static char pci_help_text[] =
475         "[bus] [long]\n"
476         "    - short or long list of PCI devices on bus 'bus'\n"
477 #ifdef CONFIG_CMD_PCI_ENUM
478         "pci enum\n"
479         "    - re-enumerate PCI buses\n"
480 #endif
481         "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"
490         "    - write to CFG address";
491 #endif
492
493 U_BOOT_CMD(
494         pci,    5,      1,      do_pci,
495         "list and access PCI Configuration Space", pci_help_text
496 );