blob: 0043471fc73e59cfb42e828936accaaa5c294f17 [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>
wdenkc6097192002-11-03 00:24:07 +000021#include <asm/processor.h>
22#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000023#include <pci.h>
24
Simon Glass07a58872015-11-26 19:51:20 -070025struct pci_reg_info {
26 const char *name;
27 enum pci_size_t size;
28 u8 offset;
29};
30
Simon Glass72ef5b62015-11-26 19:51:26 -070031static int pci_byte_size(enum pci_size_t size)
Simon Glass07a58872015-11-26 19:51:20 -070032{
33 switch (size) {
34 case PCI_SIZE_8:
Simon Glass72ef5b62015-11-26 19:51:26 -070035 return 1;
Simon Glass07a58872015-11-26 19:51:20 -070036 case PCI_SIZE_16:
Simon Glass72ef5b62015-11-26 19:51:26 -070037 return 2;
Simon Glass07a58872015-11-26 19:51:20 -070038 case PCI_SIZE_32:
39 default:
Simon Glass72ef5b62015-11-26 19:51:26 -070040 return 4;
Simon Glass07a58872015-11-26 19:51:20 -070041 }
42}
43
Simon Glass72ef5b62015-11-26 19:51:26 -070044static int pci_field_width(enum pci_size_t size)
45{
46 return pci_byte_size(size) * 2;
47}
48
Simon Glasscab24b32015-11-26 19:51:29 -070049#ifdef CONFIG_DM_PCI
50static 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}
61#else
Simon Glass07a58872015-11-26 19:51:20 -070062static unsigned long pci_read_config(pci_dev_t dev, int offset,
63 enum pci_size_t size)
64{
65 u32 val32;
66 u16 val16;
67 u8 val8;
68
69 switch (size) {
70 case PCI_SIZE_8:
71 pci_read_config_byte(dev, offset, &val8);
72 return val8;
73 case PCI_SIZE_16:
74 pci_read_config_word(dev, offset, &val16);
75 return val16;
76 case PCI_SIZE_32:
77 default:
78 pci_read_config_dword(dev, offset, &val32);
79 return val32;
80 }
81}
82
83static void pci_show_regs(pci_dev_t dev, struct pci_reg_info *regs)
84{
85 for (; regs->name; regs++) {
86 printf(" %s =%*s%#.*lx\n", regs->name,
87 (int)(28 - strlen(regs->name)), "",
88 pci_field_width(regs->size),
89 pci_read_config(dev, regs->offset, regs->size));
90 }
91}
Simon Glasscab24b32015-11-26 19:51:29 -070092#endif
Simon Glass07a58872015-11-26 19:51:20 -070093
Yehuda Yitschake5f96a82016-12-01 17:14:18 +020094#ifdef CONFIG_DM_PCI
95int pci_bar_show(struct udevice *dev)
96{
97 u8 header_type;
98 int bar_cnt, bar_id, mem_type;
99 bool is_64, is_io;
100 u32 base_low, base_high;
101 u32 size_low, size_high;
102 u64 base, size;
103 u32 reg_addr;
104 int prefetchable;
105
106 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
107
108 if (header_type == PCI_HEADER_TYPE_CARDBUS) {
109 printf("CardBus doesn't support BARs\n");
110 return -ENOSYS;
111 }
112
113 bar_cnt = (header_type == PCI_HEADER_TYPE_NORMAL) ? 6 : 2;
114
115 printf("ID Base Size Width Type\n");
116 printf("----------------------------------------------------------\n");
117
118 bar_id = 0;
119 reg_addr = PCI_BASE_ADDRESS_0;
120 while (bar_cnt) {
121 dm_pci_read_config32(dev, reg_addr, &base_low);
122 dm_pci_write_config32(dev, reg_addr, 0xffffffff);
123 dm_pci_read_config32(dev, reg_addr, &size_low);
124 dm_pci_write_config32(dev, reg_addr, base_low);
125 reg_addr += 4;
126
127 base = base_low & ~0xf;
128 size = size_low & ~0xf;
129 base_high = 0x0;
130 size_high = 0xffffffff;
131 is_64 = 0;
132 prefetchable = base_low & PCI_BASE_ADDRESS_MEM_PREFETCH;
133 is_io = base_low & PCI_BASE_ADDRESS_SPACE_IO;
134 mem_type = base_low & PCI_BASE_ADDRESS_MEM_TYPE_MASK;
135
136 if (mem_type == PCI_BASE_ADDRESS_MEM_TYPE_64) {
137 dm_pci_read_config32(dev, reg_addr, &base_high);
138 dm_pci_write_config32(dev, reg_addr, 0xffffffff);
139 dm_pci_read_config32(dev, reg_addr, &size_high);
140 dm_pci_write_config32(dev, reg_addr, base_high);
141 bar_cnt--;
142 reg_addr += 4;
143 is_64 = 1;
144 }
145
146 base = base | ((u64)base_high << 32);
147 size = size | ((u64)size_high << 32);
148
149 if ((!is_64 && size_low) || (is_64 && size)) {
150 size = ~size + 1;
Kunihiko Hayashi4ebeb4c2019-08-23 10:56:55 +0900151 printf(" %d %#018llx %#018llx %d %s %s\n",
Simon Glass84d7f912017-05-27 07:38:12 -0600152 bar_id, (unsigned long long)base,
153 (unsigned long long)size, is_64 ? 64 : 32,
Yehuda Yitschake5f96a82016-12-01 17:14:18 +0200154 is_io ? "I/O" : "MEM",
155 prefetchable ? "Prefetchable" : "");
156 }
157
158 bar_id++;
159 bar_cnt--;
160 }
161
162 return 0;
163}
164#endif
165
Simon Glass07a58872015-11-26 19:51:20 -0700166static struct pci_reg_info regs_start[] = {
167 { "vendor ID", PCI_SIZE_16, PCI_VENDOR_ID },
168 { "device ID", PCI_SIZE_16, PCI_DEVICE_ID },
169 { "command register ID", PCI_SIZE_16, PCI_COMMAND },
170 { "status register", PCI_SIZE_16, PCI_STATUS },
171 { "revision ID", PCI_SIZE_8, PCI_REVISION_ID },
172 {},
173};
174
175static struct pci_reg_info regs_rest[] = {
176 { "sub class code", PCI_SIZE_8, PCI_CLASS_SUB_CODE },
177 { "programming interface", PCI_SIZE_8, PCI_CLASS_PROG },
178 { "cache line", PCI_SIZE_8, PCI_CACHE_LINE_SIZE },
179 { "latency time", PCI_SIZE_8, PCI_LATENCY_TIMER },
180 { "header type", PCI_SIZE_8, PCI_HEADER_TYPE },
181 { "BIST", PCI_SIZE_8, PCI_BIST },
182 { "base address 0", PCI_SIZE_32, PCI_BASE_ADDRESS_0 },
183 {},
184};
185
186static struct pci_reg_info regs_normal[] = {
187 { "base address 1", PCI_SIZE_32, PCI_BASE_ADDRESS_1 },
188 { "base address 2", PCI_SIZE_32, PCI_BASE_ADDRESS_2 },
189 { "base address 3", PCI_SIZE_32, PCI_BASE_ADDRESS_3 },
190 { "base address 4", PCI_SIZE_32, PCI_BASE_ADDRESS_4 },
191 { "base address 5", PCI_SIZE_32, PCI_BASE_ADDRESS_5 },
192 { "cardBus CIS pointer", PCI_SIZE_32, PCI_CARDBUS_CIS },
193 { "sub system vendor ID", PCI_SIZE_16, PCI_SUBSYSTEM_VENDOR_ID },
194 { "sub system ID", PCI_SIZE_16, PCI_SUBSYSTEM_ID },
195 { "expansion ROM base address", PCI_SIZE_32, PCI_ROM_ADDRESS },
196 { "interrupt line", PCI_SIZE_8, PCI_INTERRUPT_LINE },
197 { "interrupt pin", PCI_SIZE_8, PCI_INTERRUPT_PIN },
198 { "min Grant", PCI_SIZE_8, PCI_MIN_GNT },
199 { "max Latency", PCI_SIZE_8, PCI_MAX_LAT },
200 {},
201};
202
203static struct pci_reg_info regs_bridge[] = {
204 { "base address 1", PCI_SIZE_32, PCI_BASE_ADDRESS_1 },
205 { "primary bus number", PCI_SIZE_8, PCI_PRIMARY_BUS },
206 { "secondary bus number", PCI_SIZE_8, PCI_SECONDARY_BUS },
207 { "subordinate bus number", PCI_SIZE_8, PCI_SUBORDINATE_BUS },
208 { "secondary latency timer", PCI_SIZE_8, PCI_SEC_LATENCY_TIMER },
209 { "IO base", PCI_SIZE_8, PCI_IO_BASE },
210 { "IO limit", PCI_SIZE_8, PCI_IO_LIMIT },
211 { "secondary status", PCI_SIZE_16, PCI_SEC_STATUS },
212 { "memory base", PCI_SIZE_16, PCI_MEMORY_BASE },
213 { "memory limit", PCI_SIZE_16, PCI_MEMORY_LIMIT },
214 { "prefetch memory base", PCI_SIZE_16, PCI_PREF_MEMORY_BASE },
215 { "prefetch memory limit", PCI_SIZE_16, PCI_PREF_MEMORY_LIMIT },
216 { "prefetch memory base upper", PCI_SIZE_32, PCI_PREF_BASE_UPPER32 },
217 { "prefetch memory limit upper", PCI_SIZE_32, PCI_PREF_LIMIT_UPPER32 },
218 { "IO base upper 16 bits", PCI_SIZE_16, PCI_IO_BASE_UPPER16 },
219 { "IO limit upper 16 bits", PCI_SIZE_16, PCI_IO_LIMIT_UPPER16 },
220 { "expansion ROM base address", PCI_SIZE_32, PCI_ROM_ADDRESS1 },
221 { "interrupt line", PCI_SIZE_8, PCI_INTERRUPT_LINE },
222 { "interrupt pin", PCI_SIZE_8, PCI_INTERRUPT_PIN },
223 { "bridge control", PCI_SIZE_16, PCI_BRIDGE_CONTROL },
224 {},
225};
226
227static struct pci_reg_info regs_cardbus[] = {
228 { "capabilities", PCI_SIZE_8, PCI_CB_CAPABILITY_LIST },
229 { "secondary status", PCI_SIZE_16, PCI_CB_SEC_STATUS },
230 { "primary bus number", PCI_SIZE_8, PCI_CB_PRIMARY_BUS },
231 { "CardBus number", PCI_SIZE_8, PCI_CB_CARD_BUS },
232 { "subordinate bus number", PCI_SIZE_8, PCI_CB_SUBORDINATE_BUS },
233 { "CardBus latency timer", PCI_SIZE_8, PCI_CB_LATENCY_TIMER },
234 { "CardBus memory base 0", PCI_SIZE_32, PCI_CB_MEMORY_BASE_0 },
235 { "CardBus memory limit 0", PCI_SIZE_32, PCI_CB_MEMORY_LIMIT_0 },
236 { "CardBus memory base 1", PCI_SIZE_32, PCI_CB_MEMORY_BASE_1 },
237 { "CardBus memory limit 1", PCI_SIZE_32, PCI_CB_MEMORY_LIMIT_1 },
238 { "CardBus IO base 0", PCI_SIZE_16, PCI_CB_IO_BASE_0 },
239 { "CardBus IO base high 0", PCI_SIZE_16, PCI_CB_IO_BASE_0_HI },
240 { "CardBus IO limit 0", PCI_SIZE_16, PCI_CB_IO_LIMIT_0 },
241 { "CardBus IO limit high 0", PCI_SIZE_16, PCI_CB_IO_LIMIT_0_HI },
242 { "CardBus IO base 1", PCI_SIZE_16, PCI_CB_IO_BASE_1 },
243 { "CardBus IO base high 1", PCI_SIZE_16, PCI_CB_IO_BASE_1_HI },
244 { "CardBus IO limit 1", PCI_SIZE_16, PCI_CB_IO_LIMIT_1 },
245 { "CardBus IO limit high 1", PCI_SIZE_16, PCI_CB_IO_LIMIT_1_HI },
246 { "interrupt line", PCI_SIZE_8, PCI_INTERRUPT_LINE },
247 { "interrupt pin", PCI_SIZE_8, PCI_INTERRUPT_PIN },
248 { "bridge control", PCI_SIZE_16, PCI_CB_BRIDGE_CONTROL },
249 { "subvendor ID", PCI_SIZE_16, PCI_CB_SUBSYSTEM_VENDOR_ID },
250 { "subdevice ID", PCI_SIZE_16, PCI_CB_SUBSYSTEM_ID },
251 { "PC Card 16bit base address", PCI_SIZE_32, PCI_CB_LEGACY_MODE_BASE },
252 {},
253};
254
Simon Glassc2be0702015-11-26 19:51:25 -0700255/**
256 * pci_header_show() - Show the header of the specified PCI device.
wdenkc6097192002-11-03 00:24:07 +0000257 *
Simon Glassc2be0702015-11-26 19:51:25 -0700258 * @dev: Bus+Device+Function number
wdenkc6097192002-11-03 00:24:07 +0000259 */
Simon Glasscab24b32015-11-26 19:51:29 -0700260#ifdef CONFIG_DM_PCI
261void pci_header_show(struct udevice *dev)
262#else
wdenkc6097192002-11-03 00:24:07 +0000263void pci_header_show(pci_dev_t dev)
Simon Glasscab24b32015-11-26 19:51:29 -0700264#endif
wdenkc6097192002-11-03 00:24:07 +0000265{
Simon Glasscab24b32015-11-26 19:51:29 -0700266#ifdef CONFIG_DM_PCI
267 unsigned long class, header_type;
268
269 dm_pci_read_config(dev, PCI_CLASS_CODE, &class, PCI_SIZE_8);
270 dm_pci_read_config(dev, PCI_HEADER_TYPE, &header_type, PCI_SIZE_8);
271#else
Simon Glass07a58872015-11-26 19:51:20 -0700272 u8 class, header_type;
wdenkc6097192002-11-03 00:24:07 +0000273
Simon Glass49f835f2015-11-26 19:51:24 -0700274 pci_read_config_byte(dev, PCI_CLASS_CODE, &class);
wdenkc6097192002-11-03 00:24:07 +0000275 pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
Simon Glasscab24b32015-11-26 19:51:29 -0700276#endif
Simon Glass07a58872015-11-26 19:51:20 -0700277 pci_show_regs(dev, regs_start);
Simon Glasscab24b32015-11-26 19:51:29 -0700278 printf(" class code = 0x%.2x (%s)\n", (int)class,
Simon Glass07a58872015-11-26 19:51:20 -0700279 pci_class_str(class));
280 pci_show_regs(dev, regs_rest);
wdenkc6097192002-11-03 00:24:07 +0000281
wdenk7c7a23b2002-12-07 00:20:59 +0000282 switch (header_type & 0x03) {
283 case PCI_HEADER_TYPE_NORMAL: /* "normal" PCI device */
Simon Glass07a58872015-11-26 19:51:20 -0700284 pci_show_regs(dev, regs_normal);
wdenk7c7a23b2002-12-07 00:20:59 +0000285 break;
wdenk7c7a23b2002-12-07 00:20:59 +0000286 case PCI_HEADER_TYPE_BRIDGE: /* PCI-to-PCI bridge */
Simon Glass07a58872015-11-26 19:51:20 -0700287 pci_show_regs(dev, regs_bridge);
wdenk7c7a23b2002-12-07 00:20:59 +0000288 break;
wdenk7c7a23b2002-12-07 00:20:59 +0000289 case PCI_HEADER_TYPE_CARDBUS: /* PCI-to-CardBus bridge */
Simon Glass07a58872015-11-26 19:51:20 -0700290 pci_show_regs(dev, regs_cardbus);
wdenk7c7a23b2002-12-07 00:20:59 +0000291 break;
wdenk8bde7f72003-06-27 21:31:46 +0000292
wdenk7c7a23b2002-12-07 00:20:59 +0000293 default:
294 printf("unknown header\n");
wdenk8bde7f72003-06-27 21:31:46 +0000295 break;
wdenkc6097192002-11-03 00:24:07 +0000296 }
wdenkc6097192002-11-03 00:24:07 +0000297}
298
Simon Glassc4f32bb2015-11-26 19:51:28 -0700299void pciinfo_header(int busnum, bool short_listing)
300{
301 printf("Scanning PCI devices on bus %d\n", busnum);
302
303 if (short_listing) {
304 printf("BusDevFun VendorId DeviceId Device Class Sub-Class\n");
305 printf("_____________________________________________________________\n");
306 }
307}
308
Simon Glasscab24b32015-11-26 19:51:29 -0700309#ifdef CONFIG_DM_PCI
310/**
311 * pci_header_show_brief() - Show the short-form PCI device header
312 *
313 * Reads and prints the header of the specified PCI device in short form.
314 *
315 * @dev: PCI device to show
316 */
317static void pci_header_show_brief(struct udevice *dev)
318{
319 ulong vendor, device;
320 ulong class, subclass;
321
322 dm_pci_read_config(dev, PCI_VENDOR_ID, &vendor, PCI_SIZE_16);
323 dm_pci_read_config(dev, PCI_DEVICE_ID, &device, PCI_SIZE_16);
324 dm_pci_read_config(dev, PCI_CLASS_CODE, &class, PCI_SIZE_8);
325 dm_pci_read_config(dev, PCI_CLASS_SUB_CODE, &subclass, PCI_SIZE_8);
326
327 printf("0x%.4lx 0x%.4lx %-23s 0x%.2lx\n",
328 vendor, device,
329 pci_class_str(class), subclass);
330}
331
332static void pciinfo(struct udevice *bus, bool short_listing)
333{
334 struct udevice *dev;
335
336 pciinfo_header(bus->seq, short_listing);
337
338 for (device_find_first_child(bus, &dev);
339 dev;
340 device_find_next_child(&dev)) {
341 struct pci_child_platdata *pplat;
342
343 pplat = dev_get_parent_platdata(dev);
344 if (short_listing) {
345 printf("%02x.%02x.%02x ", bus->seq,
346 PCI_DEV(pplat->devfn), PCI_FUNC(pplat->devfn));
347 pci_header_show_brief(dev);
348 } else {
349 printf("\nFound PCI device %02x.%02x.%02x:\n", bus->seq,
350 PCI_DEV(pplat->devfn), PCI_FUNC(pplat->devfn));
351 pci_header_show(dev);
352 }
353 }
354}
355
356#else
357
Simon Glassc2be0702015-11-26 19:51:25 -0700358/**
359 * pci_header_show_brief() - Show the short-form PCI device header
Simon Glass49f835f2015-11-26 19:51:24 -0700360 *
Simon Glassc2be0702015-11-26 19:51:25 -0700361 * Reads and prints the header of the specified PCI device in short form.
Simon Glass49f835f2015-11-26 19:51:24 -0700362 *
Simon Glassc2be0702015-11-26 19:51:25 -0700363 * @dev: Bus+Device+Function number
Simon Glass49f835f2015-11-26 19:51:24 -0700364 */
365void pci_header_show_brief(pci_dev_t dev)
366{
367 u16 vendor, device;
368 u8 class, subclass;
369
370 pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
371 pci_read_config_word(dev, PCI_DEVICE_ID, &device);
372 pci_read_config_byte(dev, PCI_CLASS_CODE, &class);
373 pci_read_config_byte(dev, PCI_CLASS_SUB_CODE, &subclass);
374
375 printf("0x%.4x 0x%.4x %-23s 0x%.2x\n",
376 vendor, device,
377 pci_class_str(class), subclass);
378}
379
Simon Glassc2be0702015-11-26 19:51:25 -0700380/**
381 * pciinfo() - Show a list of devices on the PCI bus
Simon Glass49f835f2015-11-26 19:51:24 -0700382 *
Simon Glassc2be0702015-11-26 19:51:25 -0700383 * Show information about devices on PCI bus. Depending on @short_pci_listing
384 * the output will be more or less exhaustive.
Simon Glass49f835f2015-11-26 19:51:24 -0700385 *
Simon Glassc2be0702015-11-26 19:51:25 -0700386 * @bus_num: The number of the bus to be scanned
387 * @short_pci_listing: true to use short form, showing only a brief header
388 * for each device
Simon Glass49f835f2015-11-26 19:51:24 -0700389 */
390void pciinfo(int bus_num, int short_pci_listing)
391{
392 struct pci_controller *hose = pci_bus_to_hose(bus_num);
Simon Glassc2be0702015-11-26 19:51:25 -0700393 int device;
394 int function;
395 unsigned char header_type;
396 unsigned short vendor_id;
Simon Glass49f835f2015-11-26 19:51:24 -0700397 pci_dev_t dev;
398 int ret;
399
400 if (!hose)
401 return;
402
Simon Glassc4f32bb2015-11-26 19:51:28 -0700403 pciinfo_header(bus_num, short_pci_listing);
Simon Glass49f835f2015-11-26 19:51:24 -0700404
Simon Glassc2be0702015-11-26 19:51:25 -0700405 for (device = 0; device < PCI_MAX_PCI_DEVICES; device++) {
406 header_type = 0;
407 vendor_id = 0;
408 for (function = 0; function < PCI_MAX_PCI_FUNCTIONS;
409 function++) {
Simon Glass49f835f2015-11-26 19:51:24 -0700410 /*
411 * If this is not a multi-function device, we skip
412 * the rest.
413 */
Simon Glassc2be0702015-11-26 19:51:25 -0700414 if (function && !(header_type & 0x80))
Simon Glass49f835f2015-11-26 19:51:24 -0700415 break;
416
Simon Glassc2be0702015-11-26 19:51:25 -0700417 dev = PCI_BDF(bus_num, device, function);
Simon Glass49f835f2015-11-26 19:51:24 -0700418
419 if (pci_skip_dev(hose, dev))
420 continue;
421
422 ret = pci_read_config_word(dev, PCI_VENDOR_ID,
Simon Glassc2be0702015-11-26 19:51:25 -0700423 &vendor_id);
Simon Glass49f835f2015-11-26 19:51:24 -0700424 if (ret)
425 goto error;
Simon Glassc2be0702015-11-26 19:51:25 -0700426 if ((vendor_id == 0xFFFF) || (vendor_id == 0x0000))
Simon Glass49f835f2015-11-26 19:51:24 -0700427 continue;
428
Simon Glassc2be0702015-11-26 19:51:25 -0700429 if (!function) {
Simon Glass49f835f2015-11-26 19:51:24 -0700430 pci_read_config_byte(dev, PCI_HEADER_TYPE,
Simon Glassc2be0702015-11-26 19:51:25 -0700431 &header_type);
Simon Glass49f835f2015-11-26 19:51:24 -0700432 }
433
434 if (short_pci_listing) {
Simon Glassc2be0702015-11-26 19:51:25 -0700435 printf("%02x.%02x.%02x ", bus_num, device,
436 function);
Simon Glass49f835f2015-11-26 19:51:24 -0700437 pci_header_show_brief(dev);
438 } else {
439 printf("\nFound PCI device %02x.%02x.%02x:\n",
Simon Glassc2be0702015-11-26 19:51:25 -0700440 bus_num, device, function);
Simon Glass49f835f2015-11-26 19:51:24 -0700441 pci_header_show(dev);
442 }
443 }
444 }
445
446 return;
447error:
448 printf("Cannot read bus configuration: %d\n", ret);
449}
Simon Glasscab24b32015-11-26 19:51:29 -0700450#endif
Simon Glass49f835f2015-11-26 19:51:24 -0700451
Simon Glassc2be0702015-11-26 19:51:25 -0700452/**
453 * get_pci_dev() - Convert the "bus.device.function" identifier into a number
454 *
455 * @name: Device string in the form "bus.device.function" where each is in hex
456 * @return encoded pci_dev_t or -1 if the string was invalid
wdenkc6097192002-11-03 00:24:07 +0000457 */
Simon Glassc2be0702015-11-26 19:51:25 -0700458static pci_dev_t get_pci_dev(char *name)
wdenkc6097192002-11-03 00:24:07 +0000459{
460 char cnum[12];
461 int len, i, iold, n;
462 int bdfs[3] = {0,0,0};
463
464 len = strlen(name);
465 if (len > 8)
466 return -1;
467 for (i = 0, iold = 0, n = 0; i < len; i++) {
468 if (name[i] == '.') {
469 memcpy(cnum, &name[iold], i - iold);
470 cnum[i - iold] = '\0';
471 bdfs[n++] = simple_strtoul(cnum, NULL, 16);
472 iold = i + 1;
473 }
474 }
475 strcpy(cnum, &name[iold]);
476 if (n == 0)
477 n = 1;
478 bdfs[n] = simple_strtoul(cnum, NULL, 16);
Simon Glassc2be0702015-11-26 19:51:25 -0700479
wdenkc6097192002-11-03 00:24:07 +0000480 return PCI_BDF(bdfs[0], bdfs[1], bdfs[2]);
481}
482
Simon Glasscab24b32015-11-26 19:51:29 -0700483#ifdef CONFIG_DM_PCI
484static int pci_cfg_display(struct udevice *dev, ulong addr,
485 enum pci_size_t size, ulong length)
486#else
Simon Glass72ef5b62015-11-26 19:51:26 -0700487static int pci_cfg_display(pci_dev_t bdf, ulong addr, enum pci_size_t size,
488 ulong length)
Simon Glasscab24b32015-11-26 19:51:29 -0700489#endif
wdenkc6097192002-11-03 00:24:07 +0000490{
491#define DISP_LINE_LEN 16
492 ulong i, nbytes, linebytes;
Simon Glass72ef5b62015-11-26 19:51:26 -0700493 int byte_size;
wdenkc6097192002-11-03 00:24:07 +0000494 int rc = 0;
495
Simon Glass72ef5b62015-11-26 19:51:26 -0700496 byte_size = pci_byte_size(size);
wdenkc6097192002-11-03 00:24:07 +0000497 if (length == 0)
Simon Glass72ef5b62015-11-26 19:51:26 -0700498 length = 0x40 / byte_size; /* Standard PCI config space */
wdenkc6097192002-11-03 00:24:07 +0000499
500 /* Print the lines.
501 * once, and all accesses are with the specified bus width.
502 */
Simon Glass72ef5b62015-11-26 19:51:26 -0700503 nbytes = length * byte_size;
wdenkc6097192002-11-03 00:24:07 +0000504 do {
wdenkc6097192002-11-03 00:24:07 +0000505 printf("%08lx:", addr);
Simon Glass72ef5b62015-11-26 19:51:26 -0700506 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
507 for (i = 0; i < linebytes; i += byte_size) {
508 unsigned long val;
509
Simon Glasscab24b32015-11-26 19:51:29 -0700510#ifdef CONFIG_DM_PCI
511 dm_pci_read_config(dev, addr, &val, size);
512#else
Simon Glass72ef5b62015-11-26 19:51:26 -0700513 val = pci_read_config(bdf, addr, size);
Simon Glasscab24b32015-11-26 19:51:29 -0700514#endif
Simon Glass72ef5b62015-11-26 19:51:26 -0700515 printf(" %0*lx", pci_field_width(size), val);
516 addr += byte_size;
wdenkc6097192002-11-03 00:24:07 +0000517 }
518 printf("\n");
519 nbytes -= linebytes;
520 if (ctrlc()) {
521 rc = 1;
522 break;
523 }
524 } while (nbytes > 0);
525
526 return (rc);
527}
528
Simon Glasscab24b32015-11-26 19:51:29 -0700529#ifndef CONFIG_DM_PCI
wdenkc6097192002-11-03 00:24:07 +0000530static int pci_cfg_write (pci_dev_t bdf, ulong addr, ulong size, ulong value)
531{
532 if (size == 4) {
533 pci_write_config_dword(bdf, addr, value);
534 }
535 else if (size == 2) {
536 ushort val = value & 0xffff;
537 pci_write_config_word(bdf, addr, val);
538 }
539 else {
540 u_char val = value & 0xff;
541 pci_write_config_byte(bdf, addr, val);
542 }
543 return 0;
544}
Simon Glasscab24b32015-11-26 19:51:29 -0700545#endif
wdenkc6097192002-11-03 00:24:07 +0000546
Simon Glasscab24b32015-11-26 19:51:29 -0700547#ifdef CONFIG_DM_PCI
548static int pci_cfg_modify(struct udevice *dev, ulong addr, ulong size,
Simon Glass72ef5b62015-11-26 19:51:26 -0700549 ulong value, int incrflag)
Simon Glasscab24b32015-11-26 19:51:29 -0700550#else
551static int pci_cfg_modify(pci_dev_t bdf, ulong addr, ulong size, ulong value,
552 int incrflag)
553#endif
wdenkc6097192002-11-03 00:24:07 +0000554{
555 ulong i;
556 int nbytes;
Simon Glass72ef5b62015-11-26 19:51:26 -0700557 ulong val;
wdenkc6097192002-11-03 00:24:07 +0000558
559 /* Print the address, followed by value. Then accept input for
560 * the next value. A non-converted value exits.
561 */
562 do {
563 printf("%08lx:", addr);
Simon Glasscab24b32015-11-26 19:51:29 -0700564#ifdef CONFIG_DM_PCI
565 dm_pci_read_config(dev, addr, &val, size);
566#else
Simon Glass72ef5b62015-11-26 19:51:26 -0700567 val = pci_read_config(bdf, addr, size);
Simon Glasscab24b32015-11-26 19:51:29 -0700568#endif
Simon Glass72ef5b62015-11-26 19:51:26 -0700569 printf(" %0*lx", pci_field_width(size), val);
wdenkc6097192002-11-03 00:24:07 +0000570
Simon Glasse1bf8242014-04-10 20:01:27 -0600571 nbytes = cli_readline(" ? ");
wdenkc6097192002-11-03 00:24:07 +0000572 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
573 /* <CR> pressed as only input, don't modify current
574 * location and move to next. "-" pressed will go back.
575 */
576 if (incrflag)
577 addr += nbytes ? -size : size;
578 nbytes = 1;
Simon Glassb26440f2014-04-10 20:01:31 -0600579 /* good enough to not time out */
580 bootretry_reset_cmd_timeout();
wdenkc6097192002-11-03 00:24:07 +0000581 }
582#ifdef CONFIG_BOOT_RETRY_TIME
583 else if (nbytes == -2) {
584 break; /* timed out, exit the command */
585 }
586#endif
587 else {
588 char *endp;
589 i = simple_strtoul(console_buffer, &endp, 16);
590 nbytes = endp - console_buffer;
591 if (nbytes) {
wdenkc6097192002-11-03 00:24:07 +0000592 /* good enough to not time out
593 */
Simon Glassb26440f2014-04-10 20:01:31 -0600594 bootretry_reset_cmd_timeout();
Simon Glasscab24b32015-11-26 19:51:29 -0700595#ifdef CONFIG_DM_PCI
596 dm_pci_write_config(dev, addr, i, size);
597#else
598 pci_cfg_write(bdf, addr, size, i);
599#endif
wdenkc6097192002-11-03 00:24:07 +0000600 if (incrflag)
601 addr += size;
602 }
603 }
604 } while (nbytes);
605
606 return 0;
607}
608
Simon Glassb997a732017-04-08 13:10:06 -0600609#ifdef CONFIG_DM_PCI
610static const struct pci_flag_info {
611 uint flag;
612 const char *name;
613} pci_flag_info[] = {
614 { PCI_REGION_IO, "io" },
615 { PCI_REGION_PREFETCH, "prefetch" },
616 { PCI_REGION_SYS_MEMORY, "sysmem" },
617 { PCI_REGION_RO, "readonly" },
618 { PCI_REGION_IO, "io" },
619};
620
621static void pci_show_regions(struct udevice *bus)
622{
623 struct pci_controller *hose = dev_get_uclass_priv(bus);
624 const struct pci_region *reg;
625 int i, j;
626
627 if (!hose) {
628 printf("Bus '%s' is not a PCI controller\n", bus->name);
629 return;
630 }
631
Kunihiko Hayashi4ebeb4c2019-08-23 10:56:55 +0900632 printf("# %-18s %-18s %-18s %s\n", "Bus start", "Phys start", "Size",
Simon Glassb997a732017-04-08 13:10:06 -0600633 "Flags");
634 for (i = 0, reg = hose->regions; i < hose->region_count; i++, reg++) {
Kunihiko Hayashi4ebeb4c2019-08-23 10:56:55 +0900635 printf("%d %#018llx %#018llx %#018llx ", i,
Simon Glassb997a732017-04-08 13:10:06 -0600636 (unsigned long long)reg->bus_start,
637 (unsigned long long)reg->phys_start,
638 (unsigned long long)reg->size);
639 if (!(reg->flags & PCI_REGION_TYPE))
640 printf("mem ");
641 for (j = 0; j < ARRAY_SIZE(pci_flag_info); j++) {
642 if (reg->flags & pci_flag_info[j].flag)
643 printf("%s ", pci_flag_info[j].name);
644 }
645 printf("\n");
646 }
647}
648#endif
649
wdenkc6097192002-11-03 00:24:07 +0000650/* PCI Configuration Space access commands
651 *
652 * Syntax:
653 * pci display[.b, .w, .l] bus.device.function} [addr] [len]
654 * pci next[.b, .w, .l] bus.device.function [addr]
655 * pci modify[.b, .w, .l] bus.device.function [addr]
656 * pci write[.b, .w, .l] bus.device.function addr value
657 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000658static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc6097192002-11-03 00:24:07 +0000659{
Simon Glass72ef5b62015-11-26 19:51:26 -0700660 ulong addr = 0, value = 0, cmd_size = 0;
661 enum pci_size_t size = PCI_SIZE_32;
Simon Glasscab24b32015-11-26 19:51:29 -0700662#ifdef CONFIG_DM_PCI
663 struct udevice *dev, *bus;
664#else
Simon Glass32ec5b32015-11-26 19:51:27 -0700665 pci_dev_t dev;
Simon Glasscab24b32015-11-26 19:51:29 -0700666#endif
Simon Glassca7de762015-11-26 19:51:19 -0700667 int busnum = 0;
wdenkc6097192002-11-03 00:24:07 +0000668 pci_dev_t bdf = 0;
669 char cmd = 's';
Simon Glassbfa41912015-11-26 19:51:18 -0700670 int ret = 0;
wdenkc6097192002-11-03 00:24:07 +0000671
672 if (argc > 1)
673 cmd = argv[1][0];
674
675 switch (cmd) {
676 case 'd': /* display */
677 case 'n': /* next */
678 case 'm': /* modify */
679 case 'w': /* write */
680 /* Check for a size specification. */
Simon Glass72ef5b62015-11-26 19:51:26 -0700681 cmd_size = cmd_get_data_size(argv[1], 4);
682 size = (cmd_size == 4) ? PCI_SIZE_32 : cmd_size - 1;
wdenkc6097192002-11-03 00:24:07 +0000683 if (argc > 3)
684 addr = simple_strtoul(argv[3], NULL, 16);
685 if (argc > 4)
686 value = simple_strtoul(argv[4], NULL, 16);
687 case 'h': /* header */
Yehuda Yitschake5f96a82016-12-01 17:14:18 +0200688#ifdef CONFIG_DM_PCI
689 case 'b': /* bars */
690#endif
wdenkc6097192002-11-03 00:24:07 +0000691 if (argc < 3)
692 goto usage;
693 if ((bdf = get_pci_dev(argv[2])) == -1)
694 return 1;
695 break;
Simon Glass18642422017-08-04 16:34:35 -0600696#if defined(CONFIG_DM_PCI)
John Schmoller96d61602010-10-22 00:20:23 -0500697 case 'e':
Stephen Warrene578b922016-01-26 11:10:11 -0700698 pci_init();
699 return 0;
John Schmoller96d61602010-10-22 00:20:23 -0500700#endif
Simon Glassb997a732017-04-08 13:10:06 -0600701 case 'r': /* no break */
wdenkc6097192002-11-03 00:24:07 +0000702 default: /* scan bus */
703 value = 1; /* short listing */
wdenkc6097192002-11-03 00:24:07 +0000704 if (argc > 1) {
Simon Glassb997a732017-04-08 13:10:06 -0600705 if (cmd != 'r' && argv[argc-1][0] == 'l') {
wdenkc6097192002-11-03 00:24:07 +0000706 value = 0;
707 argc--;
708 }
709 if (argc > 1)
Simon Glassca7de762015-11-26 19:51:19 -0700710 busnum = simple_strtoul(argv[1], NULL, 16);
wdenkc6097192002-11-03 00:24:07 +0000711 }
Simon Glasscab24b32015-11-26 19:51:29 -0700712#ifdef CONFIG_DM_PCI
713 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, &bus);
714 if (ret) {
715 printf("No such bus\n");
716 return CMD_RET_FAILURE;
717 }
Simon Glassb997a732017-04-08 13:10:06 -0600718 if (cmd == 'r')
719 pci_show_regions(bus);
720 else
721 pciinfo(bus, value);
Simon Glasscab24b32015-11-26 19:51:29 -0700722#else
Simon Glassca7de762015-11-26 19:51:19 -0700723 pciinfo(busnum, value);
Simon Glasscab24b32015-11-26 19:51:29 -0700724#endif
wdenkc6097192002-11-03 00:24:07 +0000725 return 0;
726 }
727
Simon Glasscab24b32015-11-26 19:51:29 -0700728#ifdef CONFIG_DM_PCI
Simon Glassf3f1fae2015-11-29 13:17:48 -0700729 ret = dm_pci_bus_find_bdf(bdf, &dev);
Simon Glasscab24b32015-11-26 19:51:29 -0700730 if (ret) {
731 printf("No such device\n");
732 return CMD_RET_FAILURE;
733 }
734#else
Simon Glass32ec5b32015-11-26 19:51:27 -0700735 dev = bdf;
Simon Glasscab24b32015-11-26 19:51:29 -0700736#endif
Simon Glass32ec5b32015-11-26 19:51:27 -0700737
wdenkc6097192002-11-03 00:24:07 +0000738 switch (argv[1][0]) {
739 case 'h': /* header */
Simon Glass32ec5b32015-11-26 19:51:27 -0700740 pci_header_show(dev);
Simon Glassbfa41912015-11-26 19:51:18 -0700741 break;
wdenkc6097192002-11-03 00:24:07 +0000742 case 'd': /* display */
Simon Glass32ec5b32015-11-26 19:51:27 -0700743 return pci_cfg_display(dev, addr, size, value);
wdenkc6097192002-11-03 00:24:07 +0000744 case 'n': /* next */
745 if (argc < 4)
746 goto usage;
Simon Glass32ec5b32015-11-26 19:51:27 -0700747 ret = pci_cfg_modify(dev, addr, size, value, 0);
Simon Glassbfa41912015-11-26 19:51:18 -0700748 break;
wdenkc6097192002-11-03 00:24:07 +0000749 case 'm': /* modify */
750 if (argc < 4)
751 goto usage;
Simon Glass32ec5b32015-11-26 19:51:27 -0700752 ret = pci_cfg_modify(dev, addr, size, value, 1);
Simon Glassbfa41912015-11-26 19:51:18 -0700753 break;
wdenkc6097192002-11-03 00:24:07 +0000754 case 'w': /* write */
755 if (argc < 5)
756 goto usage;
Simon Glasscab24b32015-11-26 19:51:29 -0700757#ifdef CONFIG_DM_PCI
758 ret = dm_pci_write_config(dev, addr, value, size);
759#else
Simon Glass32ec5b32015-11-26 19:51:27 -0700760 ret = pci_cfg_write(dev, addr, size, value);
Simon Glasscab24b32015-11-26 19:51:29 -0700761#endif
Simon Glassbfa41912015-11-26 19:51:18 -0700762 break;
Yehuda Yitschake5f96a82016-12-01 17:14:18 +0200763#ifdef CONFIG_DM_PCI
764
765 case 'b': /* bars */
766 return pci_bar_show(dev);
767#endif
Simon Glassbfa41912015-11-26 19:51:18 -0700768 default:
769 ret = CMD_RET_USAGE;
770 break;
wdenkc6097192002-11-03 00:24:07 +0000771 }
772
Simon Glassbfa41912015-11-26 19:51:18 -0700773 return ret;
wdenkc6097192002-11-03 00:24:07 +0000774 usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +0000775 return CMD_RET_USAGE;
wdenkc6097192002-11-03 00:24:07 +0000776}
777
wdenk8bde7f72003-06-27 21:31:46 +0000778/***************************************************/
779
Kim Phillips088f1b12012-10-29 13:34:31 +0000780#ifdef CONFIG_SYS_LONGHELP
781static char pci_help_text[] =
wdenk8bde7f72003-06-27 21:31:46 +0000782 "[bus] [long]\n"
783 " - short or long list of PCI devices on bus 'bus'\n"
Simon Glass18642422017-08-04 16:34:35 -0600784#if defined(CONFIG_DM_PCI)
John Schmoller96d61602010-10-22 00:20:23 -0500785 "pci enum\n"
Stephen Warrene578b922016-01-26 11:10:11 -0700786 " - Enumerate PCI buses\n"
John Schmoller96d61602010-10-22 00:20:23 -0500787#endif
wdenk8bde7f72003-06-27 21:31:46 +0000788 "pci header b.d.f\n"
789 " - show header of PCI device 'bus.device.function'\n"
Yehuda Yitschake5f96a82016-12-01 17:14:18 +0200790#ifdef CONFIG_DM_PCI
791 "pci bar b.d.f\n"
792 " - show BARs base and size for device b.d.f'\n"
Simon Glassb997a732017-04-08 13:10:06 -0600793 "pci regions\n"
794 " - show PCI regions\n"
Yehuda Yitschake5f96a82016-12-01 17:14:18 +0200795#endif
wdenk8bde7f72003-06-27 21:31:46 +0000796 "pci display[.b, .w, .l] b.d.f [address] [# of objects]\n"
797 " - display PCI configuration space (CFG)\n"
798 "pci next[.b, .w, .l] b.d.f address\n"
799 " - modify, read and keep CFG address\n"
800 "pci modify[.b, .w, .l] b.d.f address\n"
801 " - modify, auto increment CFG address\n"
802 "pci write[.b, .w, .l] b.d.f address value\n"
Kim Phillips088f1b12012-10-29 13:34:31 +0000803 " - write to CFG address";
804#endif
805
806U_BOOT_CMD(
807 pci, 5, 1, do_pci,
808 "list and access PCI Configuration Space", pci_help_text
wdenk8bde7f72003-06-27 21:31:46 +0000809);