cmd_pci.c 15 KB

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