cmd_pci.c 15 KB

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