blob: 3b1863f139c94e14828ab26202adebf40c592001 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
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.
wdenkc6097192002-11-03 00:24:07 +00009 */
10
11/*
12 * PCI routines
13 */
14
15#include <common.h>
Simon Glass0098e172014-04-10 20:01:30 -060016#include <bootretry.h>
Simon Glass18d66532014-04-10 20:01:25 -060017#include <cli.h>
wdenkc6097192002-11-03 00:24:07 +000018#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070019#include <console.h>
Simon Glasscab24b32015-11-26 19:51:29 -070020#include <dm.h>
Simon Glass691d7192020-05-10 11:40:02 -060021#include <init.h>
wdenkc6097192002-11-03 00:24:07 +000022#include <asm/processor.h>
23#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000024#include <pci.h>
25
Simon Glass07a58872015-11-26 19:51:20 -070026struct pci_reg_info {
27 const char *name;
28 enum pci_size_t size;
29 u8 offset;
30};
31
Simon Glass72ef5b62015-11-26 19:51:26 -070032static int pci_byte_size(enum pci_size_t size)
Simon Glass07a58872015-11-26 19:51:20 -070033{
34 switch (size) {
35 case PCI_SIZE_8:
Simon Glass72ef5b62015-11-26 19:51:26 -070036 return 1;
Simon Glass07a58872015-11-26 19:51:20 -070037 case PCI_SIZE_16:
Simon Glass72ef5b62015-11-26 19:51:26 -070038 return 2;
Simon Glass07a58872015-11-26 19:51:20 -070039 case PCI_SIZE_32:
40 default:
Simon Glass72ef5b62015-11-26 19:51:26 -070041 return 4;
Simon Glass07a58872015-11-26 19:51:20 -070042 }
43}
44
Simon Glass72ef5b62015-11-26 19:51:26 -070045static int pci_field_width(enum pci_size_t size)
46{
47 return pci_byte_size(size) * 2;
48}
49
Simon Glasscab24b32015-11-26 19:51:29 -070050static 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 Glass07a58872015-11-26 19:51:20 -070061
Vladimir Olteanf5164f62021-09-17 15:11:22 +030062static int pci_bar_show(struct udevice *dev)
Yehuda Yitschake5f96a82016-12-01 17:14:18 +020063{
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);
Pali Roháre6335d32021-10-07 14:51:00 +020074 header_type &= 0x7f;
Yehuda Yitschake5f96a82016-12-01 17:14:18 +020075
76 if (header_type == PCI_HEADER_TYPE_CARDBUS) {
77 printf("CardBus doesn't support BARs\n");
78 return -ENOSYS;
Pali Roháre6335d32021-10-07 14:51:00 +020079 } else if (header_type != PCI_HEADER_TYPE_NORMAL &&
80 header_type != PCI_HEADER_TYPE_BRIDGE) {
81 printf("unknown header type\n");
82 return -ENOSYS;
Yehuda Yitschake5f96a82016-12-01 17:14:18 +020083 }
84
85 bar_cnt = (header_type == PCI_HEADER_TYPE_NORMAL) ? 6 : 2;
86
87 printf("ID Base Size Width Type\n");
88 printf("----------------------------------------------------------\n");
89
90 bar_id = 0;
91 reg_addr = PCI_BASE_ADDRESS_0;
92 while (bar_cnt) {
93 dm_pci_read_config32(dev, reg_addr, &base_low);
94 dm_pci_write_config32(dev, reg_addr, 0xffffffff);
95 dm_pci_read_config32(dev, reg_addr, &size_low);
96 dm_pci_write_config32(dev, reg_addr, base_low);
97 reg_addr += 4;
98
99 base = base_low & ~0xf;
100 size = size_low & ~0xf;
101 base_high = 0x0;
102 size_high = 0xffffffff;
103 is_64 = 0;
104 prefetchable = base_low & PCI_BASE_ADDRESS_MEM_PREFETCH;
105 is_io = base_low & PCI_BASE_ADDRESS_SPACE_IO;
106 mem_type = base_low & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
107
108 if (mem_type == PCI_BASE_ADDRESS_MEM_TYPE_64) {
109 dm_pci_read_config32(dev, reg_addr, &base_high);
110 dm_pci_write_config32(dev, reg_addr, 0xffffffff);
111 dm_pci_read_config32(dev, reg_addr, &size_high);
112 dm_pci_write_config32(dev, reg_addr, base_high);
113 bar_cnt--;
114 reg_addr += 4;
115 is_64 = 1;
116 }
117
118 base = base | ((u64)base_high << 32);
119 size = size | ((u64)size_high << 32);
120
121 if ((!is_64 && size_low) || (is_64 && size)) {
122 size = ~size + 1;
Kunihiko Hayashi4ebeb4c2019-08-23 10:56:55 +0900123 printf(" %d %#018llx %#018llx %d %s %s\n",
Simon Glass84d7f912017-05-27 07:38:12 -0600124 bar_id, (unsigned long long)base,
125 (unsigned long long)size, is_64 ? 64 : 32,
Yehuda Yitschake5f96a82016-12-01 17:14:18 +0200126 is_io ? "I/O" : "MEM",
127 prefetchable ? "Prefetchable" : "");
128 }
129
130 bar_id++;
131 bar_cnt--;
132 }
133
134 return 0;
135}
Yehuda Yitschake5f96a82016-12-01 17:14:18 +0200136
Simon Glass07a58872015-11-26 19:51:20 -0700137static struct pci_reg_info regs_start[] = {
138 { "vendor ID", PCI_SIZE_16, PCI_VENDOR_ID },
139 { "device ID", PCI_SIZE_16, PCI_DEVICE_ID },
140 { "command register ID", PCI_SIZE_16, PCI_COMMAND },
141 { "status register", PCI_SIZE_16, PCI_STATUS },
142 { "revision ID", PCI_SIZE_8, PCI_REVISION_ID },
143 {},
144};
145
146static struct pci_reg_info regs_rest[] = {
147 { "sub class code", PCI_SIZE_8, PCI_CLASS_SUB_CODE },
148 { "programming interface", PCI_SIZE_8, PCI_CLASS_PROG },
149 { "cache line", PCI_SIZE_8, PCI_CACHE_LINE_SIZE },
150 { "latency time", PCI_SIZE_8, PCI_LATENCY_TIMER },
151 { "header type", PCI_SIZE_8, PCI_HEADER_TYPE },
152 { "BIST", PCI_SIZE_8, PCI_BIST },
153 { "base address 0", PCI_SIZE_32, PCI_BASE_ADDRESS_0 },
154 {},
155};
156
157static struct pci_reg_info regs_normal[] = {
158 { "base address 1", PCI_SIZE_32, PCI_BASE_ADDRESS_1 },
159 { "base address 2", PCI_SIZE_32, PCI_BASE_ADDRESS_2 },
160 { "base address 3", PCI_SIZE_32, PCI_BASE_ADDRESS_3 },
161 { "base address 4", PCI_SIZE_32, PCI_BASE_ADDRESS_4 },
162 { "base address 5", PCI_SIZE_32, PCI_BASE_ADDRESS_5 },
163 { "cardBus CIS pointer", PCI_SIZE_32, PCI_CARDBUS_CIS },
164 { "sub system vendor ID", PCI_SIZE_16, PCI_SUBSYSTEM_VENDOR_ID },
165 { "sub system ID", PCI_SIZE_16, PCI_SUBSYSTEM_ID },
166 { "expansion ROM base address", PCI_SIZE_32, PCI_ROM_ADDRESS },
167 { "interrupt line", PCI_SIZE_8, PCI_INTERRUPT_LINE },
168 { "interrupt pin", PCI_SIZE_8, PCI_INTERRUPT_PIN },
169 { "min Grant", PCI_SIZE_8, PCI_MIN_GNT },
170 { "max Latency", PCI_SIZE_8, PCI_MAX_LAT },
171 {},
172};
173
174static struct pci_reg_info regs_bridge[] = {
175 { "base address 1", PCI_SIZE_32, PCI_BASE_ADDRESS_1 },
176 { "primary bus number", PCI_SIZE_8, PCI_PRIMARY_BUS },
177 { "secondary bus number", PCI_SIZE_8, PCI_SECONDARY_BUS },
178 { "subordinate bus number", PCI_SIZE_8, PCI_SUBORDINATE_BUS },
179 { "secondary latency timer", PCI_SIZE_8, PCI_SEC_LATENCY_TIMER },
180 { "IO base", PCI_SIZE_8, PCI_IO_BASE },
181 { "IO limit", PCI_SIZE_8, PCI_IO_LIMIT },
182 { "secondary status", PCI_SIZE_16, PCI_SEC_STATUS },
183 { "memory base", PCI_SIZE_16, PCI_MEMORY_BASE },
184 { "memory limit", PCI_SIZE_16, PCI_MEMORY_LIMIT },
185 { "prefetch memory base", PCI_SIZE_16, PCI_PREF_MEMORY_BASE },
186 { "prefetch memory limit", PCI_SIZE_16, PCI_PREF_MEMORY_LIMIT },
187 { "prefetch memory base upper", PCI_SIZE_32, PCI_PREF_BASE_UPPER32 },
188 { "prefetch memory limit upper", PCI_SIZE_32, PCI_PREF_LIMIT_UPPER32 },
189 { "IO base upper 16 bits", PCI_SIZE_16, PCI_IO_BASE_UPPER16 },
190 { "IO limit upper 16 bits", PCI_SIZE_16, PCI_IO_LIMIT_UPPER16 },
191 { "expansion ROM base address", PCI_SIZE_32, PCI_ROM_ADDRESS1 },
192 { "interrupt line", PCI_SIZE_8, PCI_INTERRUPT_LINE },
193 { "interrupt pin", PCI_SIZE_8, PCI_INTERRUPT_PIN },
194 { "bridge control", PCI_SIZE_16, PCI_BRIDGE_CONTROL },
195 {},
196};
197
198static struct pci_reg_info regs_cardbus[] = {
199 { "capabilities", PCI_SIZE_8, PCI_CB_CAPABILITY_LIST },
200 { "secondary status", PCI_SIZE_16, PCI_CB_SEC_STATUS },
201 { "primary bus number", PCI_SIZE_8, PCI_CB_PRIMARY_BUS },
202 { "CardBus number", PCI_SIZE_8, PCI_CB_CARD_BUS },
203 { "subordinate bus number", PCI_SIZE_8, PCI_CB_SUBORDINATE_BUS },
204 { "CardBus latency timer", PCI_SIZE_8, PCI_CB_LATENCY_TIMER },
205 { "CardBus memory base 0", PCI_SIZE_32, PCI_CB_MEMORY_BASE_0 },
206 { "CardBus memory limit 0", PCI_SIZE_32, PCI_CB_MEMORY_LIMIT_0 },
207 { "CardBus memory base 1", PCI_SIZE_32, PCI_CB_MEMORY_BASE_1 },
208 { "CardBus memory limit 1", PCI_SIZE_32, PCI_CB_MEMORY_LIMIT_1 },
209 { "CardBus IO base 0", PCI_SIZE_16, PCI_CB_IO_BASE_0 },
210 { "CardBus IO base high 0", PCI_SIZE_16, PCI_CB_IO_BASE_0_HI },
211 { "CardBus IO limit 0", PCI_SIZE_16, PCI_CB_IO_LIMIT_0 },
212 { "CardBus IO limit high 0", PCI_SIZE_16, PCI_CB_IO_LIMIT_0_HI },
213 { "CardBus IO base 1", PCI_SIZE_16, PCI_CB_IO_BASE_1 },
214 { "CardBus IO base high 1", PCI_SIZE_16, PCI_CB_IO_BASE_1_HI },
215 { "CardBus IO limit 1", PCI_SIZE_16, PCI_CB_IO_LIMIT_1 },
216 { "CardBus IO limit high 1", PCI_SIZE_16, PCI_CB_IO_LIMIT_1_HI },
217 { "interrupt line", PCI_SIZE_8, PCI_INTERRUPT_LINE },
218 { "interrupt pin", PCI_SIZE_8, PCI_INTERRUPT_PIN },
219 { "bridge control", PCI_SIZE_16, PCI_CB_BRIDGE_CONTROL },
220 { "subvendor ID", PCI_SIZE_16, PCI_CB_SUBSYSTEM_VENDOR_ID },
221 { "subdevice ID", PCI_SIZE_16, PCI_CB_SUBSYSTEM_ID },
222 { "PC Card 16bit base address", PCI_SIZE_32, PCI_CB_LEGACY_MODE_BASE },
223 {},
224};
225
Simon Glassc2be0702015-11-26 19:51:25 -0700226/**
227 * pci_header_show() - Show the header of the specified PCI device.
wdenkc6097192002-11-03 00:24:07 +0000228 *
Simon Glassc2be0702015-11-26 19:51:25 -0700229 * @dev: Bus+Device+Function number
wdenkc6097192002-11-03 00:24:07 +0000230 */
Vladimir Olteana95f8ee2021-09-17 15:11:23 +0300231static void pci_header_show(struct udevice *dev)
wdenkc6097192002-11-03 00:24:07 +0000232{
Simon Glasscab24b32015-11-26 19:51:29 -0700233 unsigned long class, header_type;
234
235 dm_pci_read_config(dev, PCI_CLASS_CODE, &class, PCI_SIZE_8);
236 dm_pci_read_config(dev, PCI_HEADER_TYPE, &header_type, PCI_SIZE_8);
Simon Glass07a58872015-11-26 19:51:20 -0700237 pci_show_regs(dev, regs_start);
Simon Glasscab24b32015-11-26 19:51:29 -0700238 printf(" class code = 0x%.2x (%s)\n", (int)class,
Simon Glass07a58872015-11-26 19:51:20 -0700239 pci_class_str(class));
240 pci_show_regs(dev, regs_rest);
wdenkc6097192002-11-03 00:24:07 +0000241
Pali Rohár43dad072021-10-07 14:51:01 +0200242 switch (header_type & 0x7f) {
wdenk7c7a23b2002-12-07 00:20:59 +0000243 case PCI_HEADER_TYPE_NORMAL: /* "normal" PCI device */
Simon Glass07a58872015-11-26 19:51:20 -0700244 pci_show_regs(dev, regs_normal);
wdenk7c7a23b2002-12-07 00:20:59 +0000245 break;
wdenk7c7a23b2002-12-07 00:20:59 +0000246 case PCI_HEADER_TYPE_BRIDGE: /* PCI-to-PCI bridge */
Simon Glass07a58872015-11-26 19:51:20 -0700247 pci_show_regs(dev, regs_bridge);
wdenk7c7a23b2002-12-07 00:20:59 +0000248 break;
wdenk7c7a23b2002-12-07 00:20:59 +0000249 case PCI_HEADER_TYPE_CARDBUS: /* PCI-to-CardBus bridge */
Simon Glass07a58872015-11-26 19:51:20 -0700250 pci_show_regs(dev, regs_cardbus);
wdenk7c7a23b2002-12-07 00:20:59 +0000251 break;
wdenk8bde7f72003-06-27 21:31:46 +0000252
wdenk7c7a23b2002-12-07 00:20:59 +0000253 default:
254 printf("unknown header\n");
wdenk8bde7f72003-06-27 21:31:46 +0000255 break;
wdenkc6097192002-11-03 00:24:07 +0000256 }
wdenkc6097192002-11-03 00:24:07 +0000257}
258
Vladimir Oltean577fd582021-09-17 15:11:24 +0300259static void pciinfo_header(int busnum, bool short_listing)
Simon Glassc4f32bb2015-11-26 19:51:28 -0700260{
261 printf("Scanning PCI devices on bus %d\n", busnum);
262
263 if (short_listing) {
264 printf("BusDevFun VendorId DeviceId Device Class Sub-Class\n");
265 printf("_____________________________________________________________\n");
266 }
267}
268
Simon Glasscab24b32015-11-26 19:51:29 -0700269/**
270 * pci_header_show_brief() - Show the short-form PCI device header
271 *
272 * Reads and prints the header of the specified PCI device in short form.
273 *
274 * @dev: PCI device to show
275 */
276static void pci_header_show_brief(struct udevice *dev)
277{
278 ulong vendor, device;
279 ulong class, subclass;
280
281 dm_pci_read_config(dev, PCI_VENDOR_ID, &vendor, PCI_SIZE_16);
282 dm_pci_read_config(dev, PCI_DEVICE_ID, &device, PCI_SIZE_16);
283 dm_pci_read_config(dev, PCI_CLASS_CODE, &class, PCI_SIZE_8);
284 dm_pci_read_config(dev, PCI_CLASS_SUB_CODE, &subclass, PCI_SIZE_8);
285
286 printf("0x%.4lx 0x%.4lx %-23s 0x%.2lx\n",
287 vendor, device,
288 pci_class_str(class), subclass);
289}
290
291static void pciinfo(struct udevice *bus, bool short_listing)
292{
293 struct udevice *dev;
294
Simon Glass8b85dfc2020-12-16 21:20:07 -0700295 pciinfo_header(dev_seq(bus), short_listing);
Simon Glasscab24b32015-11-26 19:51:29 -0700296
297 for (device_find_first_child(bus, &dev);
298 dev;
299 device_find_next_child(&dev)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700300 struct pci_child_plat *pplat;
Simon Glasscab24b32015-11-26 19:51:29 -0700301
Simon Glasscaa4daa2020-12-03 16:55:18 -0700302 pplat = dev_get_parent_plat(dev);
Simon Glasscab24b32015-11-26 19:51:29 -0700303 if (short_listing) {
Simon Glass8b85dfc2020-12-16 21:20:07 -0700304 printf("%02x.%02x.%02x ", dev_seq(bus),
Simon Glasscab24b32015-11-26 19:51:29 -0700305 PCI_DEV(pplat->devfn), PCI_FUNC(pplat->devfn));
306 pci_header_show_brief(dev);
307 } else {
Simon Glass8b85dfc2020-12-16 21:20:07 -0700308 printf("\nFound PCI device %02x.%02x.%02x:\n",
309 dev_seq(bus),
Simon Glasscab24b32015-11-26 19:51:29 -0700310 PCI_DEV(pplat->devfn), PCI_FUNC(pplat->devfn));
311 pci_header_show(dev);
312 }
313 }
314}
315
Simon Glassc2be0702015-11-26 19:51:25 -0700316/**
317 * get_pci_dev() - Convert the "bus.device.function" identifier into a number
318 *
319 * @name: Device string in the form "bus.device.function" where each is in hex
320 * @return encoded pci_dev_t or -1 if the string was invalid
wdenkc6097192002-11-03 00:24:07 +0000321 */
Simon Glassc2be0702015-11-26 19:51:25 -0700322static pci_dev_t get_pci_dev(char *name)
wdenkc6097192002-11-03 00:24:07 +0000323{
324 char cnum[12];
325 int len, i, iold, n;
326 int bdfs[3] = {0,0,0};
327
328 len = strlen(name);
329 if (len > 8)
330 return -1;
331 for (i = 0, iold = 0, n = 0; i < len; i++) {
332 if (name[i] == '.') {
333 memcpy(cnum, &name[iold], i - iold);
334 cnum[i - iold] = '\0';
Simon Glass7e5f4602021-07-24 09:03:29 -0600335 bdfs[n++] = hextoul(cnum, NULL);
wdenkc6097192002-11-03 00:24:07 +0000336 iold = i + 1;
337 }
338 }
339 strcpy(cnum, &name[iold]);
340 if (n == 0)
341 n = 1;
Simon Glass7e5f4602021-07-24 09:03:29 -0600342 bdfs[n] = hextoul(cnum, NULL);
Simon Glassc2be0702015-11-26 19:51:25 -0700343
wdenkc6097192002-11-03 00:24:07 +0000344 return PCI_BDF(bdfs[0], bdfs[1], bdfs[2]);
345}
346
Simon Glasscab24b32015-11-26 19:51:29 -0700347static int pci_cfg_display(struct udevice *dev, ulong addr,
348 enum pci_size_t size, ulong length)
wdenkc6097192002-11-03 00:24:07 +0000349{
350#define DISP_LINE_LEN 16
351 ulong i, nbytes, linebytes;
Simon Glass72ef5b62015-11-26 19:51:26 -0700352 int byte_size;
wdenkc6097192002-11-03 00:24:07 +0000353 int rc = 0;
354
Simon Glass72ef5b62015-11-26 19:51:26 -0700355 byte_size = pci_byte_size(size);
wdenkc6097192002-11-03 00:24:07 +0000356 if (length == 0)
Simon Glass72ef5b62015-11-26 19:51:26 -0700357 length = 0x40 / byte_size; /* Standard PCI config space */
wdenkc6097192002-11-03 00:24:07 +0000358
359 /* Print the lines.
360 * once, and all accesses are with the specified bus width.
361 */
Simon Glass72ef5b62015-11-26 19:51:26 -0700362 nbytes = length * byte_size;
wdenkc6097192002-11-03 00:24:07 +0000363 do {
wdenkc6097192002-11-03 00:24:07 +0000364 printf("%08lx:", addr);
Simon Glass72ef5b62015-11-26 19:51:26 -0700365 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
366 for (i = 0; i < linebytes; i += byte_size) {
367 unsigned long val;
368
Simon Glasscab24b32015-11-26 19:51:29 -0700369 dm_pci_read_config(dev, addr, &val, size);
Simon Glass72ef5b62015-11-26 19:51:26 -0700370 printf(" %0*lx", pci_field_width(size), val);
371 addr += byte_size;
wdenkc6097192002-11-03 00:24:07 +0000372 }
373 printf("\n");
374 nbytes -= linebytes;
375 if (ctrlc()) {
376 rc = 1;
377 break;
378 }
379 } while (nbytes > 0);
380
381 return (rc);
382}
383
Simon Glasscab24b32015-11-26 19:51:29 -0700384static int pci_cfg_modify(struct udevice *dev, ulong addr, ulong size,
Simon Glass72ef5b62015-11-26 19:51:26 -0700385 ulong value, int incrflag)
wdenkc6097192002-11-03 00:24:07 +0000386{
387 ulong i;
388 int nbytes;
Simon Glass72ef5b62015-11-26 19:51:26 -0700389 ulong val;
wdenkc6097192002-11-03 00:24:07 +0000390
391 /* Print the address, followed by value. Then accept input for
392 * the next value. A non-converted value exits.
393 */
394 do {
395 printf("%08lx:", addr);
Simon Glasscab24b32015-11-26 19:51:29 -0700396 dm_pci_read_config(dev, addr, &val, size);
Simon Glass72ef5b62015-11-26 19:51:26 -0700397 printf(" %0*lx", pci_field_width(size), val);
wdenkc6097192002-11-03 00:24:07 +0000398
Simon Glasse1bf8242014-04-10 20:01:27 -0600399 nbytes = cli_readline(" ? ");
wdenkc6097192002-11-03 00:24:07 +0000400 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
401 /* <CR> pressed as only input, don't modify current
402 * location and move to next. "-" pressed will go back.
403 */
404 if (incrflag)
405 addr += nbytes ? -size : size;
406 nbytes = 1;
Simon Glassb26440f2014-04-10 20:01:31 -0600407 /* good enough to not time out */
408 bootretry_reset_cmd_timeout();
wdenkc6097192002-11-03 00:24:07 +0000409 }
410#ifdef CONFIG_BOOT_RETRY_TIME
411 else if (nbytes == -2) {
412 break; /* timed out, exit the command */
413 }
414#endif
415 else {
416 char *endp;
Simon Glass7e5f4602021-07-24 09:03:29 -0600417 i = hextoul(console_buffer, &endp);
wdenkc6097192002-11-03 00:24:07 +0000418 nbytes = endp - console_buffer;
419 if (nbytes) {
wdenkc6097192002-11-03 00:24:07 +0000420 /* good enough to not time out
421 */
Simon Glassb26440f2014-04-10 20:01:31 -0600422 bootretry_reset_cmd_timeout();
Simon Glasscab24b32015-11-26 19:51:29 -0700423 dm_pci_write_config(dev, addr, i, size);
wdenkc6097192002-11-03 00:24:07 +0000424 if (incrflag)
425 addr += size;
426 }
427 }
428 } while (nbytes);
429
430 return 0;
431}
432
Simon Glassb997a732017-04-08 13:10:06 -0600433static const struct pci_flag_info {
434 uint flag;
435 const char *name;
436} pci_flag_info[] = {
437 { PCI_REGION_IO, "io" },
438 { PCI_REGION_PREFETCH, "prefetch" },
439 { PCI_REGION_SYS_MEMORY, "sysmem" },
440 { PCI_REGION_RO, "readonly" },
441 { PCI_REGION_IO, "io" },
442};
443
444static void pci_show_regions(struct udevice *bus)
445{
446 struct pci_controller *hose = dev_get_uclass_priv(bus);
447 const struct pci_region *reg;
448 int i, j;
449
450 if (!hose) {
451 printf("Bus '%s' is not a PCI controller\n", bus->name);
452 return;
453 }
454
Kunihiko Hayashi4ebeb4c2019-08-23 10:56:55 +0900455 printf("# %-18s %-18s %-18s %s\n", "Bus start", "Phys start", "Size",
Simon Glassb997a732017-04-08 13:10:06 -0600456 "Flags");
457 for (i = 0, reg = hose->regions; i < hose->region_count; i++, reg++) {
Kunihiko Hayashi4ebeb4c2019-08-23 10:56:55 +0900458 printf("%d %#018llx %#018llx %#018llx ", i,
Simon Glassb997a732017-04-08 13:10:06 -0600459 (unsigned long long)reg->bus_start,
460 (unsigned long long)reg->phys_start,
461 (unsigned long long)reg->size);
462 if (!(reg->flags & PCI_REGION_TYPE))
463 printf("mem ");
464 for (j = 0; j < ARRAY_SIZE(pci_flag_info); j++) {
465 if (reg->flags & pci_flag_info[j].flag)
466 printf("%s ", pci_flag_info[j].name);
467 }
468 printf("\n");
469 }
470}
Simon Glassb997a732017-04-08 13:10:06 -0600471
wdenkc6097192002-11-03 00:24:07 +0000472/* PCI Configuration Space access commands
473 *
474 * Syntax:
475 * pci display[.b, .w, .l] bus.device.function} [addr] [len]
476 * pci next[.b, .w, .l] bus.device.function [addr]
477 * pci modify[.b, .w, .l] bus.device.function [addr]
478 * pci write[.b, .w, .l] bus.device.function addr value
479 */
Simon Glass09140112020-05-10 11:40:03 -0600480static int do_pci(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenkc6097192002-11-03 00:24:07 +0000481{
Simon Glass72ef5b62015-11-26 19:51:26 -0700482 ulong addr = 0, value = 0, cmd_size = 0;
483 enum pci_size_t size = PCI_SIZE_32;
Simon Glasscab24b32015-11-26 19:51:29 -0700484 struct udevice *dev, *bus;
Simon Glassca7de762015-11-26 19:51:19 -0700485 int busnum = 0;
wdenkc6097192002-11-03 00:24:07 +0000486 pci_dev_t bdf = 0;
487 char cmd = 's';
Simon Glassbfa41912015-11-26 19:51:18 -0700488 int ret = 0;
wdenkc6097192002-11-03 00:24:07 +0000489
490 if (argc > 1)
491 cmd = argv[1][0];
492
493 switch (cmd) {
494 case 'd': /* display */
495 case 'n': /* next */
496 case 'm': /* modify */
497 case 'w': /* write */
498 /* Check for a size specification. */
Simon Glass72ef5b62015-11-26 19:51:26 -0700499 cmd_size = cmd_get_data_size(argv[1], 4);
500 size = (cmd_size == 4) ? PCI_SIZE_32 : cmd_size - 1;
wdenkc6097192002-11-03 00:24:07 +0000501 if (argc > 3)
Simon Glass7e5f4602021-07-24 09:03:29 -0600502 addr = hextoul(argv[3], NULL);
wdenkc6097192002-11-03 00:24:07 +0000503 if (argc > 4)
Simon Glass7e5f4602021-07-24 09:03:29 -0600504 value = hextoul(argv[4], NULL);
wdenkc6097192002-11-03 00:24:07 +0000505 case 'h': /* header */
Yehuda Yitschake5f96a82016-12-01 17:14:18 +0200506 case 'b': /* bars */
wdenkc6097192002-11-03 00:24:07 +0000507 if (argc < 3)
508 goto usage;
509 if ((bdf = get_pci_dev(argv[2])) == -1)
510 return 1;
511 break;
John Schmoller96d61602010-10-22 00:20:23 -0500512 case 'e':
Stephen Warrene578b922016-01-26 11:10:11 -0700513 pci_init();
514 return 0;
Simon Glassb997a732017-04-08 13:10:06 -0600515 case 'r': /* no break */
wdenkc6097192002-11-03 00:24:07 +0000516 default: /* scan bus */
517 value = 1; /* short listing */
wdenkc6097192002-11-03 00:24:07 +0000518 if (argc > 1) {
Simon Glassb997a732017-04-08 13:10:06 -0600519 if (cmd != 'r' && argv[argc-1][0] == 'l') {
wdenkc6097192002-11-03 00:24:07 +0000520 value = 0;
521 argc--;
522 }
523 if (argc > 1)
Simon Glass7e5f4602021-07-24 09:03:29 -0600524 busnum = hextoul(argv[1], NULL);
wdenkc6097192002-11-03 00:24:07 +0000525 }
Simon Glasscab24b32015-11-26 19:51:29 -0700526 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, &bus);
527 if (ret) {
528 printf("No such bus\n");
529 return CMD_RET_FAILURE;
530 }
Simon Glassb997a732017-04-08 13:10:06 -0600531 if (cmd == 'r')
532 pci_show_regions(bus);
533 else
534 pciinfo(bus, value);
wdenkc6097192002-11-03 00:24:07 +0000535 return 0;
536 }
537
Simon Glassf3f1fae2015-11-29 13:17:48 -0700538 ret = dm_pci_bus_find_bdf(bdf, &dev);
Simon Glasscab24b32015-11-26 19:51:29 -0700539 if (ret) {
540 printf("No such device\n");
541 return CMD_RET_FAILURE;
542 }
Simon Glass32ec5b32015-11-26 19:51:27 -0700543
wdenkc6097192002-11-03 00:24:07 +0000544 switch (argv[1][0]) {
545 case 'h': /* header */
Simon Glass32ec5b32015-11-26 19:51:27 -0700546 pci_header_show(dev);
Simon Glassbfa41912015-11-26 19:51:18 -0700547 break;
wdenkc6097192002-11-03 00:24:07 +0000548 case 'd': /* display */
Simon Glass32ec5b32015-11-26 19:51:27 -0700549 return pci_cfg_display(dev, addr, size, value);
wdenkc6097192002-11-03 00:24:07 +0000550 case 'n': /* next */
551 if (argc < 4)
552 goto usage;
Simon Glass32ec5b32015-11-26 19:51:27 -0700553 ret = pci_cfg_modify(dev, addr, size, value, 0);
Simon Glassbfa41912015-11-26 19:51:18 -0700554 break;
wdenkc6097192002-11-03 00:24:07 +0000555 case 'm': /* modify */
556 if (argc < 4)
557 goto usage;
Simon Glass32ec5b32015-11-26 19:51:27 -0700558 ret = pci_cfg_modify(dev, addr, size, value, 1);
Simon Glassbfa41912015-11-26 19:51:18 -0700559 break;
wdenkc6097192002-11-03 00:24:07 +0000560 case 'w': /* write */
561 if (argc < 5)
562 goto usage;
Simon Glasscab24b32015-11-26 19:51:29 -0700563 ret = dm_pci_write_config(dev, addr, value, size);
Simon Glassbfa41912015-11-26 19:51:18 -0700564 break;
Yehuda Yitschake5f96a82016-12-01 17:14:18 +0200565 case 'b': /* bars */
566 return pci_bar_show(dev);
Simon Glassbfa41912015-11-26 19:51:18 -0700567 default:
568 ret = CMD_RET_USAGE;
569 break;
wdenkc6097192002-11-03 00:24:07 +0000570 }
571
Simon Glassbfa41912015-11-26 19:51:18 -0700572 return ret;
wdenkc6097192002-11-03 00:24:07 +0000573 usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000574 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +0000575}
576
wdenk8bde7f72003-06-27 21:31:46 +0000577/***************************************************/
578
Kim Phillips088f1b12012-10-29 13:34:31 +0000579#ifdef CONFIG_SYS_LONGHELP
580static char pci_help_text[] =
wdenk8bde7f72003-06-27 21:31:46 +0000581 "[bus] [long]\n"
582 " - short or long list of PCI devices on bus 'bus'\n"
John Schmoller96d61602010-10-22 00:20:23 -0500583 "pci enum\n"
Stephen Warrene578b922016-01-26 11:10:11 -0700584 " - Enumerate PCI buses\n"
wdenk8bde7f72003-06-27 21:31:46 +0000585 "pci header b.d.f\n"
586 " - show header of PCI device 'bus.device.function'\n"
Yehuda Yitschake5f96a82016-12-01 17:14:18 +0200587 "pci bar b.d.f\n"
588 " - show BARs base and size for device b.d.f'\n"
Simon Glassb997a732017-04-08 13:10:06 -0600589 "pci regions\n"
590 " - show PCI regions\n"
wdenk8bde7f72003-06-27 21:31:46 +0000591 "pci display[.b, .w, .l] b.d.f [address] [# of objects]\n"
592 " - display PCI configuration space (CFG)\n"
593 "pci next[.b, .w, .l] b.d.f address\n"
594 " - modify, read and keep CFG address\n"
595 "pci modify[.b, .w, .l] b.d.f address\n"
596 " - modify, auto increment CFG address\n"
597 "pci write[.b, .w, .l] b.d.f address value\n"
Kim Phillips088f1b12012-10-29 13:34:31 +0000598 " - write to CFG address";
599#endif
600
601U_BOOT_CMD(
602 pci, 5, 1, do_pci,
603 "list and access PCI Configuration Space", pci_help_text
wdenk8bde7f72003-06-27 21:31:46 +0000604);