wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * PCI routines |
| 14 | */ |
| 15 | |
| 16 | #include <common.h> |
Simon Glass | 0098e17 | 2014-04-10 20:01:30 -0600 | [diff] [blame] | 17 | #include <bootretry.h> |
Simon Glass | 18d6653 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 18 | #include <cli.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 19 | #include <command.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 20 | #include <console.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 21 | #include <asm/processor.h> |
| 22 | #include <asm/io.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 23 | #include <pci.h> |
| 24 | |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 25 | struct pci_reg_info { |
| 26 | const char *name; |
| 27 | enum pci_size_t size; |
| 28 | u8 offset; |
| 29 | }; |
| 30 | |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 31 | static int pci_byte_size(enum pci_size_t size) |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 32 | { |
| 33 | switch (size) { |
| 34 | case PCI_SIZE_8: |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 35 | return 1; |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 36 | case PCI_SIZE_16: |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 37 | return 2; |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 38 | case PCI_SIZE_32: |
| 39 | default: |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 40 | return 4; |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 44 | static int pci_field_width(enum pci_size_t size) |
| 45 | { |
| 46 | return pci_byte_size(size) * 2; |
| 47 | } |
| 48 | |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 49 | static unsigned long pci_read_config(pci_dev_t dev, int offset, |
| 50 | enum pci_size_t size) |
| 51 | { |
| 52 | u32 val32; |
| 53 | u16 val16; |
| 54 | u8 val8; |
| 55 | |
| 56 | switch (size) { |
| 57 | case PCI_SIZE_8: |
| 58 | pci_read_config_byte(dev, offset, &val8); |
| 59 | return val8; |
| 60 | case PCI_SIZE_16: |
| 61 | pci_read_config_word(dev, offset, &val16); |
| 62 | return val16; |
| 63 | case PCI_SIZE_32: |
| 64 | default: |
| 65 | pci_read_config_dword(dev, offset, &val32); |
| 66 | return val32; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static void pci_show_regs(pci_dev_t dev, struct pci_reg_info *regs) |
| 71 | { |
| 72 | for (; regs->name; regs++) { |
| 73 | printf(" %s =%*s%#.*lx\n", regs->name, |
| 74 | (int)(28 - strlen(regs->name)), "", |
| 75 | pci_field_width(regs->size), |
| 76 | pci_read_config(dev, regs->offset, regs->size)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static struct pci_reg_info regs_start[] = { |
| 81 | { "vendor ID", PCI_SIZE_16, PCI_VENDOR_ID }, |
| 82 | { "device ID", PCI_SIZE_16, PCI_DEVICE_ID }, |
| 83 | { "command register ID", PCI_SIZE_16, PCI_COMMAND }, |
| 84 | { "status register", PCI_SIZE_16, PCI_STATUS }, |
| 85 | { "revision ID", PCI_SIZE_8, PCI_REVISION_ID }, |
| 86 | {}, |
| 87 | }; |
| 88 | |
| 89 | static struct pci_reg_info regs_rest[] = { |
| 90 | { "sub class code", PCI_SIZE_8, PCI_CLASS_SUB_CODE }, |
| 91 | { "programming interface", PCI_SIZE_8, PCI_CLASS_PROG }, |
| 92 | { "cache line", PCI_SIZE_8, PCI_CACHE_LINE_SIZE }, |
| 93 | { "latency time", PCI_SIZE_8, PCI_LATENCY_TIMER }, |
| 94 | { "header type", PCI_SIZE_8, PCI_HEADER_TYPE }, |
| 95 | { "BIST", PCI_SIZE_8, PCI_BIST }, |
| 96 | { "base address 0", PCI_SIZE_32, PCI_BASE_ADDRESS_0 }, |
| 97 | {}, |
| 98 | }; |
| 99 | |
| 100 | static struct pci_reg_info regs_normal[] = { |
| 101 | { "base address 1", PCI_SIZE_32, PCI_BASE_ADDRESS_1 }, |
| 102 | { "base address 2", PCI_SIZE_32, PCI_BASE_ADDRESS_2 }, |
| 103 | { "base address 3", PCI_SIZE_32, PCI_BASE_ADDRESS_3 }, |
| 104 | { "base address 4", PCI_SIZE_32, PCI_BASE_ADDRESS_4 }, |
| 105 | { "base address 5", PCI_SIZE_32, PCI_BASE_ADDRESS_5 }, |
| 106 | { "cardBus CIS pointer", PCI_SIZE_32, PCI_CARDBUS_CIS }, |
| 107 | { "sub system vendor ID", PCI_SIZE_16, PCI_SUBSYSTEM_VENDOR_ID }, |
| 108 | { "sub system ID", PCI_SIZE_16, PCI_SUBSYSTEM_ID }, |
| 109 | { "expansion ROM base address", PCI_SIZE_32, PCI_ROM_ADDRESS }, |
| 110 | { "interrupt line", PCI_SIZE_8, PCI_INTERRUPT_LINE }, |
| 111 | { "interrupt pin", PCI_SIZE_8, PCI_INTERRUPT_PIN }, |
| 112 | { "min Grant", PCI_SIZE_8, PCI_MIN_GNT }, |
| 113 | { "max Latency", PCI_SIZE_8, PCI_MAX_LAT }, |
| 114 | {}, |
| 115 | }; |
| 116 | |
| 117 | static struct pci_reg_info regs_bridge[] = { |
| 118 | { "base address 1", PCI_SIZE_32, PCI_BASE_ADDRESS_1 }, |
| 119 | { "primary bus number", PCI_SIZE_8, PCI_PRIMARY_BUS }, |
| 120 | { "secondary bus number", PCI_SIZE_8, PCI_SECONDARY_BUS }, |
| 121 | { "subordinate bus number", PCI_SIZE_8, PCI_SUBORDINATE_BUS }, |
| 122 | { "secondary latency timer", PCI_SIZE_8, PCI_SEC_LATENCY_TIMER }, |
| 123 | { "IO base", PCI_SIZE_8, PCI_IO_BASE }, |
| 124 | { "IO limit", PCI_SIZE_8, PCI_IO_LIMIT }, |
| 125 | { "secondary status", PCI_SIZE_16, PCI_SEC_STATUS }, |
| 126 | { "memory base", PCI_SIZE_16, PCI_MEMORY_BASE }, |
| 127 | { "memory limit", PCI_SIZE_16, PCI_MEMORY_LIMIT }, |
| 128 | { "prefetch memory base", PCI_SIZE_16, PCI_PREF_MEMORY_BASE }, |
| 129 | { "prefetch memory limit", PCI_SIZE_16, PCI_PREF_MEMORY_LIMIT }, |
| 130 | { "prefetch memory base upper", PCI_SIZE_32, PCI_PREF_BASE_UPPER32 }, |
| 131 | { "prefetch memory limit upper", PCI_SIZE_32, PCI_PREF_LIMIT_UPPER32 }, |
| 132 | { "IO base upper 16 bits", PCI_SIZE_16, PCI_IO_BASE_UPPER16 }, |
| 133 | { "IO limit upper 16 bits", PCI_SIZE_16, PCI_IO_LIMIT_UPPER16 }, |
| 134 | { "expansion ROM base address", PCI_SIZE_32, PCI_ROM_ADDRESS1 }, |
| 135 | { "interrupt line", PCI_SIZE_8, PCI_INTERRUPT_LINE }, |
| 136 | { "interrupt pin", PCI_SIZE_8, PCI_INTERRUPT_PIN }, |
| 137 | { "bridge control", PCI_SIZE_16, PCI_BRIDGE_CONTROL }, |
| 138 | {}, |
| 139 | }; |
| 140 | |
| 141 | static struct pci_reg_info regs_cardbus[] = { |
| 142 | { "capabilities", PCI_SIZE_8, PCI_CB_CAPABILITY_LIST }, |
| 143 | { "secondary status", PCI_SIZE_16, PCI_CB_SEC_STATUS }, |
| 144 | { "primary bus number", PCI_SIZE_8, PCI_CB_PRIMARY_BUS }, |
| 145 | { "CardBus number", PCI_SIZE_8, PCI_CB_CARD_BUS }, |
| 146 | { "subordinate bus number", PCI_SIZE_8, PCI_CB_SUBORDINATE_BUS }, |
| 147 | { "CardBus latency timer", PCI_SIZE_8, PCI_CB_LATENCY_TIMER }, |
| 148 | { "CardBus memory base 0", PCI_SIZE_32, PCI_CB_MEMORY_BASE_0 }, |
| 149 | { "CardBus memory limit 0", PCI_SIZE_32, PCI_CB_MEMORY_LIMIT_0 }, |
| 150 | { "CardBus memory base 1", PCI_SIZE_32, PCI_CB_MEMORY_BASE_1 }, |
| 151 | { "CardBus memory limit 1", PCI_SIZE_32, PCI_CB_MEMORY_LIMIT_1 }, |
| 152 | { "CardBus IO base 0", PCI_SIZE_16, PCI_CB_IO_BASE_0 }, |
| 153 | { "CardBus IO base high 0", PCI_SIZE_16, PCI_CB_IO_BASE_0_HI }, |
| 154 | { "CardBus IO limit 0", PCI_SIZE_16, PCI_CB_IO_LIMIT_0 }, |
| 155 | { "CardBus IO limit high 0", PCI_SIZE_16, PCI_CB_IO_LIMIT_0_HI }, |
| 156 | { "CardBus IO base 1", PCI_SIZE_16, PCI_CB_IO_BASE_1 }, |
| 157 | { "CardBus IO base high 1", PCI_SIZE_16, PCI_CB_IO_BASE_1_HI }, |
| 158 | { "CardBus IO limit 1", PCI_SIZE_16, PCI_CB_IO_LIMIT_1 }, |
| 159 | { "CardBus IO limit high 1", PCI_SIZE_16, PCI_CB_IO_LIMIT_1_HI }, |
| 160 | { "interrupt line", PCI_SIZE_8, PCI_INTERRUPT_LINE }, |
| 161 | { "interrupt pin", PCI_SIZE_8, PCI_INTERRUPT_PIN }, |
| 162 | { "bridge control", PCI_SIZE_16, PCI_CB_BRIDGE_CONTROL }, |
| 163 | { "subvendor ID", PCI_SIZE_16, PCI_CB_SUBSYSTEM_VENDOR_ID }, |
| 164 | { "subdevice ID", PCI_SIZE_16, PCI_CB_SUBSYSTEM_ID }, |
| 165 | { "PC Card 16bit base address", PCI_SIZE_32, PCI_CB_LEGACY_MODE_BASE }, |
| 166 | {}, |
| 167 | }; |
| 168 | |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 169 | /** |
| 170 | * pci_header_show() - Show the header of the specified PCI device. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 171 | * |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 172 | * @dev: Bus+Device+Function number |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 173 | */ |
| 174 | void pci_header_show(pci_dev_t dev) |
| 175 | { |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 176 | u8 class, header_type; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 177 | |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 178 | pci_read_config_byte(dev, PCI_CLASS_CODE, &class); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 179 | pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type); |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 180 | pci_show_regs(dev, regs_start); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 181 | |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 182 | printf(" class code = 0x%.2x (%s)\n", class, |
| 183 | pci_class_str(class)); |
| 184 | pci_show_regs(dev, regs_rest); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 185 | |
wdenk | 7c7a23b | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 186 | switch (header_type & 0x03) { |
| 187 | case PCI_HEADER_TYPE_NORMAL: /* "normal" PCI device */ |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 188 | pci_show_regs(dev, regs_normal); |
wdenk | 7c7a23b | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 189 | break; |
wdenk | 7c7a23b | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 190 | case PCI_HEADER_TYPE_BRIDGE: /* PCI-to-PCI bridge */ |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 191 | pci_show_regs(dev, regs_bridge); |
wdenk | 7c7a23b | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 192 | break; |
wdenk | 7c7a23b | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 193 | case PCI_HEADER_TYPE_CARDBUS: /* PCI-to-CardBus bridge */ |
Simon Glass | 07a5887 | 2015-11-26 19:51:20 -0700 | [diff] [blame] | 194 | pci_show_regs(dev, regs_cardbus); |
wdenk | 7c7a23b | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 195 | break; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 196 | |
wdenk | 7c7a23b | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 197 | default: |
| 198 | printf("unknown header\n"); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 199 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 200 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Simon Glass | c4f32bb | 2015-11-26 19:51:28 -0700 | [diff] [blame^] | 203 | void pciinfo_header(int busnum, bool short_listing) |
| 204 | { |
| 205 | printf("Scanning PCI devices on bus %d\n", busnum); |
| 206 | |
| 207 | if (short_listing) { |
| 208 | printf("BusDevFun VendorId DeviceId Device Class Sub-Class\n"); |
| 209 | printf("_____________________________________________________________\n"); |
| 210 | } |
| 211 | } |
| 212 | |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 213 | /** |
| 214 | * pci_header_show_brief() - Show the short-form PCI device header |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 215 | * |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 216 | * Reads and prints the header of the specified PCI device in short form. |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 217 | * |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 218 | * @dev: Bus+Device+Function number |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 219 | */ |
| 220 | void pci_header_show_brief(pci_dev_t dev) |
| 221 | { |
| 222 | u16 vendor, device; |
| 223 | u8 class, subclass; |
| 224 | |
| 225 | pci_read_config_word(dev, PCI_VENDOR_ID, &vendor); |
| 226 | pci_read_config_word(dev, PCI_DEVICE_ID, &device); |
| 227 | pci_read_config_byte(dev, PCI_CLASS_CODE, &class); |
| 228 | pci_read_config_byte(dev, PCI_CLASS_SUB_CODE, &subclass); |
| 229 | |
| 230 | printf("0x%.4x 0x%.4x %-23s 0x%.2x\n", |
| 231 | vendor, device, |
| 232 | pci_class_str(class), subclass); |
| 233 | } |
| 234 | |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 235 | /** |
| 236 | * pciinfo() - Show a list of devices on the PCI bus |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 237 | * |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 238 | * Show information about devices on PCI bus. Depending on @short_pci_listing |
| 239 | * the output will be more or less exhaustive. |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 240 | * |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 241 | * @bus_num: The number of the bus to be scanned |
| 242 | * @short_pci_listing: true to use short form, showing only a brief header |
| 243 | * for each device |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 244 | */ |
| 245 | void pciinfo(int bus_num, int short_pci_listing) |
| 246 | { |
| 247 | struct pci_controller *hose = pci_bus_to_hose(bus_num); |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 248 | int device; |
| 249 | int function; |
| 250 | unsigned char header_type; |
| 251 | unsigned short vendor_id; |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 252 | pci_dev_t dev; |
| 253 | int ret; |
| 254 | |
| 255 | if (!hose) |
| 256 | return; |
| 257 | |
Simon Glass | c4f32bb | 2015-11-26 19:51:28 -0700 | [diff] [blame^] | 258 | pciinfo_header(bus_num, short_pci_listing); |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 259 | |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 260 | for (device = 0; device < PCI_MAX_PCI_DEVICES; device++) { |
| 261 | header_type = 0; |
| 262 | vendor_id = 0; |
| 263 | for (function = 0; function < PCI_MAX_PCI_FUNCTIONS; |
| 264 | function++) { |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 265 | /* |
| 266 | * If this is not a multi-function device, we skip |
| 267 | * the rest. |
| 268 | */ |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 269 | if (function && !(header_type & 0x80)) |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 270 | break; |
| 271 | |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 272 | dev = PCI_BDF(bus_num, device, function); |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 273 | |
| 274 | if (pci_skip_dev(hose, dev)) |
| 275 | continue; |
| 276 | |
| 277 | ret = pci_read_config_word(dev, PCI_VENDOR_ID, |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 278 | &vendor_id); |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 279 | if (ret) |
| 280 | goto error; |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 281 | if ((vendor_id == 0xFFFF) || (vendor_id == 0x0000)) |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 282 | continue; |
| 283 | |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 284 | if (!function) { |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 285 | pci_read_config_byte(dev, PCI_HEADER_TYPE, |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 286 | &header_type); |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | if (short_pci_listing) { |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 290 | printf("%02x.%02x.%02x ", bus_num, device, |
| 291 | function); |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 292 | pci_header_show_brief(dev); |
| 293 | } else { |
| 294 | printf("\nFound PCI device %02x.%02x.%02x:\n", |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 295 | bus_num, device, function); |
Simon Glass | 49f835f | 2015-11-26 19:51:24 -0700 | [diff] [blame] | 296 | pci_header_show(dev); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return; |
| 302 | error: |
| 303 | printf("Cannot read bus configuration: %d\n", ret); |
| 304 | } |
| 305 | |
| 306 | |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 307 | /** |
| 308 | * get_pci_dev() - Convert the "bus.device.function" identifier into a number |
| 309 | * |
| 310 | * @name: Device string in the form "bus.device.function" where each is in hex |
| 311 | * @return encoded pci_dev_t or -1 if the string was invalid |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 312 | */ |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 313 | static pci_dev_t get_pci_dev(char *name) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 314 | { |
| 315 | char cnum[12]; |
| 316 | int len, i, iold, n; |
| 317 | int bdfs[3] = {0,0,0}; |
| 318 | |
| 319 | len = strlen(name); |
| 320 | if (len > 8) |
| 321 | return -1; |
| 322 | for (i = 0, iold = 0, n = 0; i < len; i++) { |
| 323 | if (name[i] == '.') { |
| 324 | memcpy(cnum, &name[iold], i - iold); |
| 325 | cnum[i - iold] = '\0'; |
| 326 | bdfs[n++] = simple_strtoul(cnum, NULL, 16); |
| 327 | iold = i + 1; |
| 328 | } |
| 329 | } |
| 330 | strcpy(cnum, &name[iold]); |
| 331 | if (n == 0) |
| 332 | n = 1; |
| 333 | bdfs[n] = simple_strtoul(cnum, NULL, 16); |
Simon Glass | c2be070 | 2015-11-26 19:51:25 -0700 | [diff] [blame] | 334 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 335 | return PCI_BDF(bdfs[0], bdfs[1], bdfs[2]); |
| 336 | } |
| 337 | |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 338 | static int pci_cfg_display(pci_dev_t bdf, ulong addr, enum pci_size_t size, |
| 339 | ulong length) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 340 | { |
| 341 | #define DISP_LINE_LEN 16 |
| 342 | ulong i, nbytes, linebytes; |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 343 | int byte_size; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 344 | int rc = 0; |
| 345 | |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 346 | byte_size = pci_byte_size(size); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 347 | if (length == 0) |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 348 | length = 0x40 / byte_size; /* Standard PCI config space */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 349 | |
| 350 | /* Print the lines. |
| 351 | * once, and all accesses are with the specified bus width. |
| 352 | */ |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 353 | nbytes = length * byte_size; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 354 | do { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 355 | printf("%08lx:", addr); |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 356 | linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes; |
| 357 | for (i = 0; i < linebytes; i += byte_size) { |
| 358 | unsigned long val; |
| 359 | |
| 360 | val = pci_read_config(bdf, addr, size); |
| 361 | printf(" %0*lx", pci_field_width(size), val); |
| 362 | addr += byte_size; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 363 | } |
| 364 | printf("\n"); |
| 365 | nbytes -= linebytes; |
| 366 | if (ctrlc()) { |
| 367 | rc = 1; |
| 368 | break; |
| 369 | } |
| 370 | } while (nbytes > 0); |
| 371 | |
| 372 | return (rc); |
| 373 | } |
| 374 | |
| 375 | static int pci_cfg_write (pci_dev_t bdf, ulong addr, ulong size, ulong value) |
| 376 | { |
| 377 | if (size == 4) { |
| 378 | pci_write_config_dword(bdf, addr, value); |
| 379 | } |
| 380 | else if (size == 2) { |
| 381 | ushort val = value & 0xffff; |
| 382 | pci_write_config_word(bdf, addr, val); |
| 383 | } |
| 384 | else { |
| 385 | u_char val = value & 0xff; |
| 386 | pci_write_config_byte(bdf, addr, val); |
| 387 | } |
| 388 | return 0; |
| 389 | } |
| 390 | |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 391 | static int pci_cfg_modify(pci_dev_t bdf, ulong addr, enum pci_size_t size, |
| 392 | ulong value, int incrflag) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 393 | { |
| 394 | ulong i; |
| 395 | int nbytes; |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 396 | ulong val; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 397 | |
| 398 | /* Print the address, followed by value. Then accept input for |
| 399 | * the next value. A non-converted value exits. |
| 400 | */ |
| 401 | do { |
| 402 | printf("%08lx:", addr); |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 403 | val = pci_read_config(bdf, addr, size); |
| 404 | printf(" %0*lx", pci_field_width(size), val); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 405 | |
Simon Glass | e1bf824 | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 406 | nbytes = cli_readline(" ? "); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 407 | if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) { |
| 408 | /* <CR> pressed as only input, don't modify current |
| 409 | * location and move to next. "-" pressed will go back. |
| 410 | */ |
| 411 | if (incrflag) |
| 412 | addr += nbytes ? -size : size; |
| 413 | nbytes = 1; |
Simon Glass | b26440f | 2014-04-10 20:01:31 -0600 | [diff] [blame] | 414 | /* good enough to not time out */ |
| 415 | bootretry_reset_cmd_timeout(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 416 | } |
| 417 | #ifdef CONFIG_BOOT_RETRY_TIME |
| 418 | else if (nbytes == -2) { |
| 419 | break; /* timed out, exit the command */ |
| 420 | } |
| 421 | #endif |
| 422 | else { |
| 423 | char *endp; |
| 424 | i = simple_strtoul(console_buffer, &endp, 16); |
| 425 | nbytes = endp - console_buffer; |
| 426 | if (nbytes) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 427 | /* good enough to not time out |
| 428 | */ |
Simon Glass | b26440f | 2014-04-10 20:01:31 -0600 | [diff] [blame] | 429 | bootretry_reset_cmd_timeout(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 430 | pci_cfg_write (bdf, addr, size, i); |
| 431 | if (incrflag) |
| 432 | addr += size; |
| 433 | } |
| 434 | } |
| 435 | } while (nbytes); |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | /* PCI Configuration Space access commands |
| 441 | * |
| 442 | * Syntax: |
| 443 | * pci display[.b, .w, .l] bus.device.function} [addr] [len] |
| 444 | * pci next[.b, .w, .l] bus.device.function [addr] |
| 445 | * pci modify[.b, .w, .l] bus.device.function [addr] |
| 446 | * pci write[.b, .w, .l] bus.device.function addr value |
| 447 | */ |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 448 | static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 449 | { |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 450 | ulong addr = 0, value = 0, cmd_size = 0; |
| 451 | enum pci_size_t size = PCI_SIZE_32; |
Simon Glass | 32ec5b3 | 2015-11-26 19:51:27 -0700 | [diff] [blame] | 452 | pci_dev_t dev; |
Simon Glass | ca7de76 | 2015-11-26 19:51:19 -0700 | [diff] [blame] | 453 | int busnum = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 454 | pci_dev_t bdf = 0; |
| 455 | char cmd = 's'; |
Simon Glass | bfa4191 | 2015-11-26 19:51:18 -0700 | [diff] [blame] | 456 | int ret = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 457 | |
| 458 | if (argc > 1) |
| 459 | cmd = argv[1][0]; |
| 460 | |
| 461 | switch (cmd) { |
| 462 | case 'd': /* display */ |
| 463 | case 'n': /* next */ |
| 464 | case 'm': /* modify */ |
| 465 | case 'w': /* write */ |
| 466 | /* Check for a size specification. */ |
Simon Glass | 72ef5b6 | 2015-11-26 19:51:26 -0700 | [diff] [blame] | 467 | cmd_size = cmd_get_data_size(argv[1], 4); |
| 468 | size = (cmd_size == 4) ? PCI_SIZE_32 : cmd_size - 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 469 | if (argc > 3) |
| 470 | addr = simple_strtoul(argv[3], NULL, 16); |
| 471 | if (argc > 4) |
| 472 | value = simple_strtoul(argv[4], NULL, 16); |
| 473 | case 'h': /* header */ |
| 474 | if (argc < 3) |
| 475 | goto usage; |
| 476 | if ((bdf = get_pci_dev(argv[2])) == -1) |
| 477 | return 1; |
| 478 | break; |
John Schmoller | 96d6160 | 2010-10-22 00:20:23 -0500 | [diff] [blame] | 479 | #ifdef CONFIG_CMD_PCI_ENUM |
| 480 | case 'e': |
| 481 | break; |
| 482 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 483 | default: /* scan bus */ |
| 484 | value = 1; /* short listing */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 485 | if (argc > 1) { |
| 486 | if (argv[argc-1][0] == 'l') { |
| 487 | value = 0; |
| 488 | argc--; |
| 489 | } |
| 490 | if (argc > 1) |
Simon Glass | ca7de76 | 2015-11-26 19:51:19 -0700 | [diff] [blame] | 491 | busnum = simple_strtoul(argv[1], NULL, 16); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 492 | } |
Simon Glass | ca7de76 | 2015-11-26 19:51:19 -0700 | [diff] [blame] | 493 | pciinfo(busnum, value); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | |
Simon Glass | 32ec5b3 | 2015-11-26 19:51:27 -0700 | [diff] [blame] | 497 | dev = bdf; |
| 498 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 499 | switch (argv[1][0]) { |
| 500 | case 'h': /* header */ |
Simon Glass | 32ec5b3 | 2015-11-26 19:51:27 -0700 | [diff] [blame] | 501 | pci_header_show(dev); |
Simon Glass | bfa4191 | 2015-11-26 19:51:18 -0700 | [diff] [blame] | 502 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 503 | case 'd': /* display */ |
Simon Glass | 32ec5b3 | 2015-11-26 19:51:27 -0700 | [diff] [blame] | 504 | return pci_cfg_display(dev, addr, size, value); |
John Schmoller | 96d6160 | 2010-10-22 00:20:23 -0500 | [diff] [blame] | 505 | #ifdef CONFIG_CMD_PCI_ENUM |
| 506 | case 'e': |
Simon Glass | 871bc92 | 2015-11-19 20:26:56 -0700 | [diff] [blame] | 507 | # ifdef CONFIG_DM_PCI |
| 508 | printf("This command is not yet supported with driver model\n"); |
| 509 | # else |
John Schmoller | 96d6160 | 2010-10-22 00:20:23 -0500 | [diff] [blame] | 510 | pci_init(); |
Simon Glass | 871bc92 | 2015-11-19 20:26:56 -0700 | [diff] [blame] | 511 | # endif |
Simon Glass | bfa4191 | 2015-11-26 19:51:18 -0700 | [diff] [blame] | 512 | break; |
John Schmoller | 96d6160 | 2010-10-22 00:20:23 -0500 | [diff] [blame] | 513 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 514 | case 'n': /* next */ |
| 515 | if (argc < 4) |
| 516 | goto usage; |
Simon Glass | 32ec5b3 | 2015-11-26 19:51:27 -0700 | [diff] [blame] | 517 | ret = pci_cfg_modify(dev, addr, size, value, 0); |
Simon Glass | bfa4191 | 2015-11-26 19:51:18 -0700 | [diff] [blame] | 518 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 519 | case 'm': /* modify */ |
| 520 | if (argc < 4) |
| 521 | goto usage; |
Simon Glass | 32ec5b3 | 2015-11-26 19:51:27 -0700 | [diff] [blame] | 522 | ret = pci_cfg_modify(dev, addr, size, value, 1); |
Simon Glass | bfa4191 | 2015-11-26 19:51:18 -0700 | [diff] [blame] | 523 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 524 | case 'w': /* write */ |
| 525 | if (argc < 5) |
| 526 | goto usage; |
Simon Glass | 32ec5b3 | 2015-11-26 19:51:27 -0700 | [diff] [blame] | 527 | ret = pci_cfg_write(dev, addr, size, value); |
Simon Glass | bfa4191 | 2015-11-26 19:51:18 -0700 | [diff] [blame] | 528 | break; |
| 529 | default: |
| 530 | ret = CMD_RET_USAGE; |
| 531 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Simon Glass | bfa4191 | 2015-11-26 19:51:18 -0700 | [diff] [blame] | 534 | return ret; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 535 | usage: |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 536 | return CMD_RET_USAGE; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 537 | } |
| 538 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 539 | /***************************************************/ |
| 540 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 541 | #ifdef CONFIG_SYS_LONGHELP |
| 542 | static char pci_help_text[] = |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 543 | "[bus] [long]\n" |
| 544 | " - short or long list of PCI devices on bus 'bus'\n" |
John Schmoller | 96d6160 | 2010-10-22 00:20:23 -0500 | [diff] [blame] | 545 | #ifdef CONFIG_CMD_PCI_ENUM |
| 546 | "pci enum\n" |
| 547 | " - re-enumerate PCI buses\n" |
| 548 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 549 | "pci header b.d.f\n" |
| 550 | " - show header of PCI device 'bus.device.function'\n" |
| 551 | "pci display[.b, .w, .l] b.d.f [address] [# of objects]\n" |
| 552 | " - display PCI configuration space (CFG)\n" |
| 553 | "pci next[.b, .w, .l] b.d.f address\n" |
| 554 | " - modify, read and keep CFG address\n" |
| 555 | "pci modify[.b, .w, .l] b.d.f address\n" |
| 556 | " - modify, auto increment CFG address\n" |
| 557 | "pci write[.b, .w, .l] b.d.f address value\n" |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 558 | " - write to CFG address"; |
| 559 | #endif |
| 560 | |
| 561 | U_BOOT_CMD( |
| 562 | pci, 5, 1, do_pci, |
| 563 | "list and access PCI Configuration Space", pci_help_text |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 564 | ); |