cmd_pci.c 15 KB

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