cmd_pci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  3. * Andreas Heppel <aheppel@sysgo.de>
  4. *
  5. * (C) Copyright 2002
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. * Wolfgang Grandegger, DENX Software Engineering, wg@denx.de.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. /*
  12. * PCI routines
  13. */
  14. #include <common.h>
  15. #include <command.h>
  16. #include <asm/processor.h>
  17. #include <asm/io.h>
  18. #include <pci.h>
  19. /*
  20. * Follows routines for the output of infos about devices on PCI bus.
  21. */
  22. void pci_header_show(pci_dev_t dev);
  23. void pci_header_show_brief(pci_dev_t dev);
  24. /*
  25. * Subroutine: pciinfo
  26. *
  27. * Description: Show information about devices on PCI bus.
  28. * Depending on the define CONFIG_SYS_SHORT_PCI_LISTING
  29. * the output will be more or less exhaustive.
  30. *
  31. * Inputs: bus_no the number of the bus to be scanned.
  32. *
  33. * Return: None
  34. *
  35. */
  36. void pciinfo(int BusNum, int ShortPCIListing)
  37. {
  38. int Device;
  39. int Function;
  40. unsigned char HeaderType;
  41. unsigned short VendorID;
  42. pci_dev_t dev;
  43. printf("Scanning PCI devices on bus %d\n", BusNum);
  44. if (ShortPCIListing) {
  45. printf("BusDevFun VendorId DeviceId Device Class Sub-Class\n");
  46. printf("_____________________________________________________________\n");
  47. }
  48. for (Device = 0; Device < PCI_MAX_PCI_DEVICES; Device++) {
  49. HeaderType = 0;
  50. VendorID = 0;
  51. for (Function = 0; Function < PCI_MAX_PCI_FUNCTIONS; Function++) {
  52. /*
  53. * If this is not a multi-function device, we skip the rest.
  54. */
  55. if (Function && !(HeaderType & 0x80))
  56. break;
  57. dev = PCI_BDF(BusNum, Device, Function);
  58. pci_read_config_word(dev, PCI_VENDOR_ID, &VendorID);
  59. if ((VendorID == 0xFFFF) || (VendorID == 0x0000))
  60. continue;
  61. if (!Function) pci_read_config_byte(dev, PCI_HEADER_TYPE, &HeaderType);
  62. if (ShortPCIListing)
  63. {
  64. printf("%02x.%02x.%02x ", BusNum, Device, Function);
  65. pci_header_show_brief(dev);
  66. }
  67. else
  68. {
  69. printf("\nFound PCI device %02x.%02x.%02x:\n",
  70. BusNum, Device, Function);
  71. pci_header_show(dev);
  72. }
  73. }
  74. }
  75. }
  76. /*
  77. * Subroutine: pci_header_show_brief
  78. *
  79. * Description: Reads and prints the header of the
  80. * specified PCI device in short form.
  81. *
  82. * Inputs: dev Bus+Device+Function number
  83. *
  84. * Return: None
  85. *
  86. */
  87. void pci_header_show_brief(pci_dev_t dev)
  88. {
  89. u16 vendor, device;
  90. u8 class, subclass;
  91. pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
  92. pci_read_config_word(dev, PCI_DEVICE_ID, &device);
  93. pci_read_config_byte(dev, PCI_CLASS_CODE, &class);
  94. pci_read_config_byte(dev, PCI_CLASS_SUB_CODE, &subclass);
  95. printf("0x%.4x 0x%.4x %-23s 0x%.2x\n",
  96. vendor, device,
  97. pci_class_str(class), subclass);
  98. }
  99. /*
  100. * Subroutine: PCI_Header_Show
  101. *
  102. * Description: Reads the header of the specified PCI device.
  103. *
  104. * Inputs: BusDevFunc Bus+Device+Function number
  105. *
  106. * Return: None
  107. *
  108. */
  109. void pci_header_show(pci_dev_t dev)
  110. {
  111. u8 _byte, header_type;
  112. u16 _word;
  113. u32 _dword;
  114. #define PRINT(msg, type, reg) \
  115. pci_read_config_##type(dev, reg, &_##type); \
  116. printf(msg, _##type)
  117. #define PRINT2(msg, type, reg, func) \
  118. pci_read_config_##type(dev, reg, &_##type); \
  119. printf(msg, _##type, func(_##type))
  120. pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
  121. PRINT (" vendor ID = 0x%.4x\n", word, PCI_VENDOR_ID);
  122. PRINT (" device ID = 0x%.4x\n", word, PCI_DEVICE_ID);
  123. PRINT (" command register = 0x%.4x\n", word, PCI_COMMAND);
  124. PRINT (" status register = 0x%.4x\n", word, PCI_STATUS);
  125. PRINT (" revision ID = 0x%.2x\n", byte, PCI_REVISION_ID);
  126. PRINT2(" class code = 0x%.2x (%s)\n", byte, PCI_CLASS_CODE,
  127. pci_class_str);
  128. PRINT (" sub class code = 0x%.2x\n", byte, PCI_CLASS_SUB_CODE);
  129. PRINT (" programming interface = 0x%.2x\n", byte, PCI_CLASS_PROG);
  130. PRINT (" cache line = 0x%.2x\n", byte, PCI_CACHE_LINE_SIZE);
  131. PRINT (" latency time = 0x%.2x\n", byte, PCI_LATENCY_TIMER);
  132. PRINT (" header type = 0x%.2x\n", byte, PCI_HEADER_TYPE);
  133. PRINT (" BIST = 0x%.2x\n", byte, PCI_BIST);
  134. PRINT (" base address 0 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_0);
  135. switch (header_type & 0x03) {
  136. case PCI_HEADER_TYPE_NORMAL: /* "normal" PCI device */
  137. PRINT (" base address 1 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_1);
  138. PRINT (" base address 2 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_2);
  139. PRINT (" base address 3 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_3);
  140. PRINT (" base address 4 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_4);
  141. PRINT (" base address 5 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_5);
  142. PRINT (" cardBus CIS pointer = 0x%.8x\n", dword, PCI_CARDBUS_CIS);
  143. PRINT (" sub system vendor ID = 0x%.4x\n", word, PCI_SUBSYSTEM_VENDOR_ID);
  144. PRINT (" sub system ID = 0x%.4x\n", word, PCI_SUBSYSTEM_ID);
  145. PRINT (" expansion ROM base address = 0x%.8x\n", dword, PCI_ROM_ADDRESS);
  146. PRINT (" interrupt line = 0x%.2x\n", byte, PCI_INTERRUPT_LINE);
  147. PRINT (" interrupt pin = 0x%.2x\n", byte, PCI_INTERRUPT_PIN);
  148. PRINT (" min Grant = 0x%.2x\n", byte, PCI_MIN_GNT);
  149. PRINT (" max Latency = 0x%.2x\n", byte, PCI_MAX_LAT);
  150. break;
  151. case PCI_HEADER_TYPE_BRIDGE: /* PCI-to-PCI bridge */
  152. PRINT (" base address 1 = 0x%.8x\n", dword, PCI_BASE_ADDRESS_1);
  153. PRINT (" primary bus number = 0x%.2x\n", byte, PCI_PRIMARY_BUS);
  154. PRINT (" secondary bus number = 0x%.2x\n", byte, PCI_SECONDARY_BUS);
  155. PRINT (" subordinate bus number = 0x%.2x\n", byte, PCI_SUBORDINATE_BUS);
  156. PRINT (" secondary latency timer = 0x%.2x\n", byte, PCI_SEC_LATENCY_TIMER);
  157. PRINT (" IO base = 0x%.2x\n", byte, PCI_IO_BASE);
  158. PRINT (" IO limit = 0x%.2x\n", byte, PCI_IO_LIMIT);
  159. PRINT (" secondary status = 0x%.4x\n", word, PCI_SEC_STATUS);
  160. PRINT (" memory base = 0x%.4x\n", word, PCI_MEMORY_BASE);
  161. PRINT (" memory limit = 0x%.4x\n", word, PCI_MEMORY_LIMIT);
  162. PRINT (" prefetch memory base = 0x%.4x\n", word, PCI_PREF_MEMORY_BASE);
  163. PRINT (" prefetch memory limit = 0x%.4x\n", word, PCI_PREF_MEMORY_LIMIT);
  164. PRINT (" prefetch memory base upper = 0x%.8x\n", dword, PCI_PREF_BASE_UPPER32);
  165. PRINT (" prefetch memory limit upper = 0x%.8x\n", dword, PCI_PREF_LIMIT_UPPER32);
  166. PRINT (" IO base upper 16 bits = 0x%.4x\n", word, PCI_IO_BASE_UPPER16);
  167. PRINT (" IO limit upper 16 bits = 0x%.4x\n", word, PCI_IO_LIMIT_UPPER16);
  168. PRINT (" expansion ROM base address = 0x%.8x\n", dword, PCI_ROM_ADDRESS1);
  169. PRINT (" interrupt line = 0x%.2x\n", byte, PCI_INTERRUPT_LINE);
  170. PRINT (" interrupt pin = 0x%.2x\n", byte, PCI_INTERRUPT_PIN);
  171. PRINT (" bridge control = 0x%.4x\n", word, PCI_BRIDGE_CONTROL);
  172. break;
  173. case PCI_HEADER_TYPE_CARDBUS: /* PCI-to-CardBus bridge */
  174. PRINT (" capabilities = 0x%.2x\n", byte, PCI_CB_CAPABILITY_LIST);
  175. PRINT (" secondary status = 0x%.4x\n", word, PCI_CB_SEC_STATUS);
  176. PRINT (" primary bus number = 0x%.2x\n", byte, PCI_CB_PRIMARY_BUS);
  177. PRINT (" CardBus number = 0x%.2x\n", byte, PCI_CB_CARD_BUS);
  178. PRINT (" subordinate bus number = 0x%.2x\n", byte, PCI_CB_SUBORDINATE_BUS);
  179. PRINT (" CardBus latency timer = 0x%.2x\n", byte, PCI_CB_LATENCY_TIMER);
  180. PRINT (" CardBus memory base 0 = 0x%.8x\n", dword, PCI_CB_MEMORY_BASE_0);
  181. PRINT (" CardBus memory limit 0 = 0x%.8x\n", dword, PCI_CB_MEMORY_LIMIT_0);
  182. PRINT (" CardBus memory base 1 = 0x%.8x\n", dword, PCI_CB_MEMORY_BASE_1);
  183. PRINT (" CardBus memory limit 1 = 0x%.8x\n", dword, PCI_CB_MEMORY_LIMIT_1);
  184. PRINT (" CardBus IO base 0 = 0x%.4x\n", word, PCI_CB_IO_BASE_0);
  185. PRINT (" CardBus IO base high 0 = 0x%.4x\n", word, PCI_CB_IO_BASE_0_HI);
  186. PRINT (" CardBus IO limit 0 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_0);
  187. PRINT (" CardBus IO limit high 0 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_0_HI);
  188. PRINT (" CardBus IO base 1 = 0x%.4x\n", word, PCI_CB_IO_BASE_1);
  189. PRINT (" CardBus IO base high 1 = 0x%.4x\n", word, PCI_CB_IO_BASE_1_HI);
  190. PRINT (" CardBus IO limit 1 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_1);
  191. PRINT (" CardBus IO limit high 1 = 0x%.4x\n", word, PCI_CB_IO_LIMIT_1_HI);
  192. PRINT (" interrupt line = 0x%.2x\n", byte, PCI_INTERRUPT_LINE);
  193. PRINT (" interrupt pin = 0x%.2x\n", byte, PCI_INTERRUPT_PIN);
  194. PRINT (" bridge control = 0x%.4x\n", word, PCI_CB_BRIDGE_CONTROL);
  195. PRINT (" subvendor ID = 0x%.4x\n", word, PCI_CB_SUBSYSTEM_VENDOR_ID);
  196. PRINT (" subdevice ID = 0x%.4x\n", word, PCI_CB_SUBSYSTEM_ID);
  197. PRINT (" PC Card 16bit base address = 0x%.8x\n", dword, PCI_CB_LEGACY_MODE_BASE);
  198. break;
  199. default:
  200. printf("unknown header\n");
  201. break;
  202. }
  203. #undef PRINT
  204. #undef PRINT2
  205. }
  206. /* Convert the "bus.device.function" identifier into a number.
  207. */
  208. static pci_dev_t get_pci_dev(char* name)
  209. {
  210. char cnum[12];
  211. int len, i, iold, n;
  212. int bdfs[3] = {0,0,0};
  213. len = strlen(name);
  214. if (len > 8)
  215. return -1;
  216. for (i = 0, iold = 0, n = 0; i < len; i++) {
  217. if (name[i] == '.') {
  218. memcpy(cnum, &name[iold], i - iold);
  219. cnum[i - iold] = '\0';
  220. bdfs[n++] = simple_strtoul(cnum, NULL, 16);
  221. iold = i + 1;
  222. }
  223. }
  224. strcpy(cnum, &name[iold]);
  225. if (n == 0)
  226. n = 1;
  227. bdfs[n] = simple_strtoul(cnum, NULL, 16);
  228. return PCI_BDF(bdfs[0], bdfs[1], bdfs[2]);
  229. }
  230. static int pci_cfg_display(pci_dev_t bdf, ulong addr, ulong size, ulong length)
  231. {
  232. #define DISP_LINE_LEN 16
  233. ulong i, nbytes, linebytes;
  234. int rc = 0;
  235. if (length == 0)
  236. length = 0x40 / size; /* Standard PCI configuration space */
  237. /* Print the lines.
  238. * once, and all accesses are with the specified bus width.
  239. */
  240. nbytes = length * size;
  241. do {
  242. uint val4;
  243. ushort val2;
  244. u_char val1;
  245. printf("%08lx:", addr);
  246. linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
  247. for (i=0; i<linebytes; i+= size) {
  248. if (size == 4) {
  249. pci_read_config_dword(bdf, addr, &val4);
  250. printf(" %08x", val4);
  251. } else if (size == 2) {
  252. pci_read_config_word(bdf, addr, &val2);
  253. printf(" %04x", val2);
  254. } else {
  255. pci_read_config_byte(bdf, addr, &val1);
  256. printf(" %02x", val1);
  257. }
  258. addr += size;
  259. }
  260. printf("\n");
  261. nbytes -= linebytes;
  262. if (ctrlc()) {
  263. rc = 1;
  264. break;
  265. }
  266. } while (nbytes > 0);
  267. return (rc);
  268. }
  269. static int pci_cfg_write (pci_dev_t bdf, ulong addr, ulong size, ulong value)
  270. {
  271. if (size == 4) {
  272. pci_write_config_dword(bdf, addr, value);
  273. }
  274. else if (size == 2) {
  275. ushort val = value & 0xffff;
  276. pci_write_config_word(bdf, addr, val);
  277. }
  278. else {
  279. u_char val = value & 0xff;
  280. pci_write_config_byte(bdf, addr, val);
  281. }
  282. return 0;
  283. }
  284. static int
  285. pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag)
  286. {
  287. ulong i;
  288. int nbytes;
  289. uint val4;
  290. ushort val2;
  291. u_char val1;
  292. /* Print the address, followed by value. Then accept input for
  293. * the next value. A non-converted value exits.
  294. */
  295. do {
  296. printf("%08lx:", addr);
  297. if (size == 4) {
  298. pci_read_config_dword(bdf, addr, &val4);
  299. printf(" %08x", val4);
  300. }
  301. else if (size == 2) {
  302. pci_read_config_word(bdf, addr, &val2);
  303. printf(" %04x", val2);
  304. }
  305. else {
  306. pci_read_config_byte(bdf, addr, &val1);
  307. printf(" %02x", val1);
  308. }
  309. nbytes = readline (" ? ");
  310. if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
  311. /* <CR> pressed as only input, don't modify current
  312. * location and move to next. "-" pressed will go back.
  313. */
  314. if (incrflag)
  315. addr += nbytes ? -size : size;
  316. nbytes = 1;
  317. #ifdef CONFIG_BOOT_RETRY_TIME
  318. reset_cmd_timeout(); /* good enough to not time out */
  319. #endif
  320. }
  321. #ifdef CONFIG_BOOT_RETRY_TIME
  322. else if (nbytes == -2) {
  323. break; /* timed out, exit the command */
  324. }
  325. #endif
  326. else {
  327. char *endp;
  328. i = simple_strtoul(console_buffer, &endp, 16);
  329. nbytes = endp - console_buffer;
  330. if (nbytes) {
  331. #ifdef CONFIG_BOOT_RETRY_TIME
  332. /* good enough to not time out
  333. */
  334. reset_cmd_timeout();
  335. #endif
  336. pci_cfg_write (bdf, addr, size, i);
  337. if (incrflag)
  338. addr += size;
  339. }
  340. }
  341. } while (nbytes);
  342. return 0;
  343. }
  344. /* PCI Configuration Space access commands
  345. *
  346. * Syntax:
  347. * pci display[.b, .w, .l] bus.device.function} [addr] [len]
  348. * pci next[.b, .w, .l] bus.device.function [addr]
  349. * pci modify[.b, .w, .l] bus.device.function [addr]
  350. * pci write[.b, .w, .l] bus.device.function addr value
  351. */
  352. static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  353. {
  354. ulong addr = 0, value = 0, size = 0;
  355. pci_dev_t bdf = 0;
  356. char cmd = 's';
  357. if (argc > 1)
  358. cmd = argv[1][0];
  359. switch (cmd) {
  360. case 'd': /* display */
  361. case 'n': /* next */
  362. case 'm': /* modify */
  363. case 'w': /* write */
  364. /* Check for a size specification. */
  365. size = cmd_get_data_size(argv[1], 4);
  366. if (argc > 3)
  367. addr = simple_strtoul(argv[3], NULL, 16);
  368. if (argc > 4)
  369. value = simple_strtoul(argv[4], NULL, 16);
  370. case 'h': /* header */
  371. if (argc < 3)
  372. goto usage;
  373. if ((bdf = get_pci_dev(argv[2])) == -1)
  374. return 1;
  375. break;
  376. #ifdef CONFIG_CMD_PCI_ENUM
  377. case 'e':
  378. break;
  379. #endif
  380. default: /* scan bus */
  381. value = 1; /* short listing */
  382. bdf = 0; /* bus number */
  383. if (argc > 1) {
  384. if (argv[argc-1][0] == 'l') {
  385. value = 0;
  386. argc--;
  387. }
  388. if (argc > 1)
  389. bdf = simple_strtoul(argv[1], NULL, 16);
  390. }
  391. pciinfo(bdf, value);
  392. return 0;
  393. }
  394. switch (argv[1][0]) {
  395. case 'h': /* header */
  396. pci_header_show(bdf);
  397. return 0;
  398. case 'd': /* display */
  399. return pci_cfg_display(bdf, addr, size, value);
  400. #ifdef CONFIG_CMD_PCI_ENUM
  401. case 'e':
  402. pci_init();
  403. return 0;
  404. #endif
  405. case 'n': /* next */
  406. if (argc < 4)
  407. goto usage;
  408. return pci_cfg_modify(bdf, addr, size, value, 0);
  409. case 'm': /* modify */
  410. if (argc < 4)
  411. goto usage;
  412. return pci_cfg_modify(bdf, addr, size, value, 1);
  413. case 'w': /* write */
  414. if (argc < 5)
  415. goto usage;
  416. return pci_cfg_write(bdf, addr, size, value);
  417. }
  418. return 1;
  419. usage:
  420. return CMD_RET_USAGE;
  421. }
  422. /***************************************************/
  423. #ifdef CONFIG_SYS_LONGHELP
  424. static char pci_help_text[] =
  425. "[bus] [long]\n"
  426. " - short or long list of PCI devices on bus 'bus'\n"
  427. #ifdef CONFIG_CMD_PCI_ENUM
  428. "pci enum\n"
  429. " - re-enumerate PCI buses\n"
  430. #endif
  431. "pci header b.d.f\n"
  432. " - show header of PCI device 'bus.device.function'\n"
  433. "pci display[.b, .w, .l] b.d.f [address] [# of objects]\n"
  434. " - display PCI configuration space (CFG)\n"
  435. "pci next[.b, .w, .l] b.d.f address\n"
  436. " - modify, read and keep CFG address\n"
  437. "pci modify[.b, .w, .l] b.d.f address\n"
  438. " - modify, auto increment CFG address\n"
  439. "pci write[.b, .w, .l] b.d.f address value\n"
  440. " - write to CFG address";
  441. #endif
  442. U_BOOT_CMD(
  443. pci, 5, 1, do_pci,
  444. "list and access PCI Configuration Space", pci_help_text
  445. );