cmd_pci.c 15 KB

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