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