pci_tests.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2009 Corey Tabaka
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <app.h>
  24. #include <debug.h>
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <compiler.h>
  30. #include <platform.h>
  31. #include <dev/pci.h>
  32. #if defined(WITH_LIB_CONSOLE)
  33. #include <lib/console.h>
  34. /*
  35. * enumerates pci devices
  36. */
  37. static void pci_list(void)
  38. {
  39. pci_location_t state;
  40. uint16_t device_id, vendor_id;
  41. uint8_t header_type;
  42. int busses = 0, devices = 0, lines = 0, devfn, ret;
  43. char c;
  44. printf("Scanning...\n");
  45. for (state.bus = 0; state.bus <= pci_get_last_bus(); state.bus++) {
  46. busses++;
  47. for (devfn = 0; devfn < 256; devfn++) {
  48. state.dev_fn = devfn;
  49. ret = pci_read_config_half(&state, PCI_CONFIG_VENDOR_ID, &vendor_id);
  50. if (ret != _PCI_SUCCESSFUL) goto error;
  51. ret = pci_read_config_half(&state, PCI_CONFIG_DEVICE_ID, &device_id);
  52. if (ret != _PCI_SUCCESSFUL) goto error;
  53. ret = pci_read_config_byte(&state, PCI_CONFIG_HEADER_TYPE, &header_type);
  54. if (ret != _PCI_SUCCESSFUL) goto error;
  55. if (vendor_id != 0xffff) {
  56. printf("%02x:%02x vendor_id=%04x device_id=%04x, header_type=%02x\n", state.bus, state.dev_fn,
  57. vendor_id, device_id, header_type);
  58. devices++;
  59. lines++;
  60. }
  61. if (~header_type & PCI_HEADER_TYPE_MULTI_FN) {
  62. // this is not a multi-function device, so advance to the next device
  63. devfn |= 7;
  64. }
  65. if (lines == 23) {
  66. printf("... press any key to continue, q to quit ...");
  67. while(getc(&c) < 0);
  68. printf("\n");
  69. lines = 0;
  70. if (c == 'q' || c == 'Q') goto quit;
  71. }
  72. }
  73. }
  74. printf("... done. Scanned %d busses, %d device/functions\n", busses, devices);
  75. quit:
  76. return;
  77. error:
  78. printf("Error while reading PCI config space: %02x\n", ret);
  79. }
  80. /*
  81. * a somewhat fugly pci config space examine/modify command. this should probably
  82. * be broken up a bit.
  83. */
  84. static int pci_config(int argc, const cmd_args *argv)
  85. {
  86. pci_location_t loc;
  87. pci_config_t config;
  88. uint32_t offset;
  89. unsigned int i;
  90. int ret;
  91. if (argc < 5) {
  92. return -1;
  93. }
  94. if (!strcmp(argv[2].str, "dump")) {
  95. loc.bus = atoui(argv[3].str);
  96. loc.dev_fn = atoui(argv[4].str);
  97. for (i=0; i < sizeof(pci_config_t); i++) {
  98. ret = pci_read_config_byte(&loc, i, (uint8_t *) &config + i);
  99. if (ret != _PCI_SUCCESSFUL) goto error;
  100. }
  101. printf("Device at %02x:%02x vendor id=%04x device id=%04x\n", loc.bus,
  102. loc.dev_fn, config.vendor_id, config.device_id);
  103. printf("command=%04x status=%04x pi=%02x sub cls=%02x base cls=%02x\n",
  104. config.command, config.status, config.program_interface,
  105. config.sub_class, config.base_class);
  106. for (i=0; i < 6; i+=2) {
  107. printf("bar%d=%08x bar%d=%08x\n", i, config.base_addresses[i],
  108. i+1, config.base_addresses[i+1]);
  109. }
  110. } else if (!strcmp(argv[2].str, "rb") || !strcmp(argv[2].str, "rh") || !strcmp(argv[2].str, "rw")) {
  111. if (argc != 6) {
  112. return -1;
  113. }
  114. loc.bus = atoui(argv[3].str);
  115. loc.dev_fn = atoui(argv[4].str);
  116. offset = atoui(argv[5].str);
  117. switch (argv[2].str[1]) {
  118. case 'b': {
  119. uint8_t value;
  120. ret = pci_read_config_byte(&loc, offset, &value);
  121. if (ret != _PCI_SUCCESSFUL) goto error;
  122. printf("byte at device %02x:%02x config offset %04x: %02x\n", loc.bus, loc.dev_fn, offset, value);
  123. }
  124. break;
  125. case 'h': {
  126. uint16_t value;
  127. ret = pci_read_config_half(&loc, offset, &value);
  128. if (ret != _PCI_SUCCESSFUL) goto error;
  129. printf("half at device %02x:%02x config offset %04x: %04x\n", loc.bus, loc.dev_fn, offset, value);
  130. }
  131. break;
  132. case 'w': {
  133. uint32_t value;
  134. ret = pci_read_config_word(&loc, offset, &value);
  135. if (ret != _PCI_SUCCESSFUL) goto error;
  136. printf("word at device %02x:%02x config offset %04x: %08x\n", loc.bus, loc.dev_fn, offset, value);
  137. }
  138. break;
  139. }
  140. } else if (!strcmp(argv[2].str, "mb") || !strcmp(argv[2].str, "mh") || !strcmp(argv[2].str, "mw")) {
  141. if (argc != 7) {
  142. return -1;
  143. }
  144. loc.bus = atoui(argv[3].str);
  145. loc.dev_fn = atoui(argv[4].str);
  146. offset = atoui(argv[5].str);
  147. switch (argv[2].str[1]) {
  148. case 'b': {
  149. uint8_t value = atoui(argv[6].str);
  150. ret = pci_write_config_byte(&loc, offset, value);
  151. if (ret != _PCI_SUCCESSFUL) goto error;
  152. printf("byte to device %02x:%02x config offset %04x: %02x\n", loc.bus, loc.dev_fn, offset, value);
  153. }
  154. break;
  155. case 'h': {
  156. uint16_t value = atoui(argv[6].str);
  157. ret = pci_write_config_half(&loc, offset, value);
  158. if (ret != _PCI_SUCCESSFUL) goto error;
  159. printf("half to device %02x:%02x config offset %04x: %04x\n", loc.bus, loc.dev_fn, offset, value);
  160. }
  161. break;
  162. case 'w': {
  163. uint32_t value = atoui(argv[6].str);
  164. ret = pci_write_config_word(&loc, offset, value);
  165. if (ret != _PCI_SUCCESSFUL) goto error;
  166. printf("word to device %02x:%02x config offset %04x: %08x\n", loc.bus, loc.dev_fn, offset, value);
  167. }
  168. break;
  169. }
  170. } else {
  171. return -1;
  172. }
  173. return 0;
  174. error:
  175. printf("Error while reading PCI config space: %02x\n", ret);
  176. return -2;
  177. }
  178. static int pci_cmd(int argc, const cmd_args *argv)
  179. {
  180. if (argc < 2) {
  181. printf("pci commands:\n");
  182. usage:
  183. printf("%s list\n", argv[0].str);
  184. printf("%s config dump <bus> <devfn>\n", argv[0].str);
  185. printf("%s config <rb|rh|rw> <bus> <devfn> <offset>\n", argv[0].str);
  186. printf("%s config <mb|mh|mw> <bus> <devfn> <offset> <value>\n", argv[0].str);
  187. goto out;
  188. }
  189. if (!strcmp(argv[1].str, "list")) {
  190. pci_list();
  191. } else if (!strcmp(argv[1].str, "config")) {
  192. if (pci_config(argc, argv)) {
  193. goto usage;
  194. }
  195. } else {
  196. goto usage;
  197. }
  198. out:
  199. return 0;
  200. }
  201. STATIC_COMMAND_START
  202. { "pci", "pci toolbox", &pci_cmd },
  203. STATIC_COMMAND_END(pcitests);
  204. #endif
  205. APP_START(pcitests)
  206. APP_END