cmd_pci.c 15 KB

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