pci_common.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2014 Google, Inc
  3. *
  4. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  5. * Andreas Heppel <aheppel@sysgo.de>
  6. *
  7. * (C) Copyright 2002, 2003
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <dm.h>
  14. #include <errno.h>
  15. #include <pci.h>
  16. #include <asm/io.h>
  17. const char *pci_class_str(u8 class)
  18. {
  19. switch (class) {
  20. case PCI_CLASS_NOT_DEFINED:
  21. return "Build before PCI Rev2.0";
  22. break;
  23. case PCI_BASE_CLASS_STORAGE:
  24. return "Mass storage controller";
  25. break;
  26. case PCI_BASE_CLASS_NETWORK:
  27. return "Network controller";
  28. break;
  29. case PCI_BASE_CLASS_DISPLAY:
  30. return "Display controller";
  31. break;
  32. case PCI_BASE_CLASS_MULTIMEDIA:
  33. return "Multimedia device";
  34. break;
  35. case PCI_BASE_CLASS_MEMORY:
  36. return "Memory controller";
  37. break;
  38. case PCI_BASE_CLASS_BRIDGE:
  39. return "Bridge device";
  40. break;
  41. case PCI_BASE_CLASS_COMMUNICATION:
  42. return "Simple comm. controller";
  43. break;
  44. case PCI_BASE_CLASS_SYSTEM:
  45. return "Base system peripheral";
  46. break;
  47. case PCI_BASE_CLASS_INPUT:
  48. return "Input device";
  49. break;
  50. case PCI_BASE_CLASS_DOCKING:
  51. return "Docking station";
  52. break;
  53. case PCI_BASE_CLASS_PROCESSOR:
  54. return "Processor";
  55. break;
  56. case PCI_BASE_CLASS_SERIAL:
  57. return "Serial bus controller";
  58. break;
  59. case PCI_BASE_CLASS_INTELLIGENT:
  60. return "Intelligent controller";
  61. break;
  62. case PCI_BASE_CLASS_SATELLITE:
  63. return "Satellite controller";
  64. break;
  65. case PCI_BASE_CLASS_CRYPT:
  66. return "Cryptographic device";
  67. break;
  68. case PCI_BASE_CLASS_SIGNAL_PROCESSING:
  69. return "DSP";
  70. break;
  71. case PCI_CLASS_OTHERS:
  72. return "Does not fit any class";
  73. break;
  74. default:
  75. return "???";
  76. break;
  77. };
  78. }
  79. pci_dev_t pci_find_class(uint find_class, int index)
  80. {
  81. int bus;
  82. int devnum;
  83. pci_dev_t bdf;
  84. uint32_t class;
  85. for (bus = 0; bus <= pci_last_busno(); bus++) {
  86. for (devnum = 0; devnum < PCI_MAX_PCI_DEVICES - 1; devnum++) {
  87. pci_read_config_dword(PCI_BDF(bus, devnum, 0),
  88. PCI_CLASS_REVISION, &class);
  89. if (class >> 16 == 0xffff)
  90. continue;
  91. for (bdf = PCI_BDF(bus, devnum, 0);
  92. bdf <= PCI_BDF(bus, devnum,
  93. PCI_MAX_PCI_FUNCTIONS - 1);
  94. bdf += PCI_BDF(0, 0, 1)) {
  95. pci_read_config_dword(bdf, PCI_CLASS_REVISION,
  96. &class);
  97. class >>= 8;
  98. if (class != find_class)
  99. continue;
  100. /*
  101. * Decrement the index. We want to return the
  102. * correct device, so index is 0 for the first
  103. * matching device, 1 for the second, etc.
  104. */
  105. if (index) {
  106. index--;
  107. continue;
  108. }
  109. /* Return index'th controller. */
  110. return bdf;
  111. }
  112. }
  113. }
  114. return -ENODEV;
  115. }
  116. __weak int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
  117. {
  118. /*
  119. * Check if pci device should be skipped in configuration
  120. */
  121. if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
  122. #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
  123. /*
  124. * Only skip configuration if "pciconfighost" is not set
  125. */
  126. if (getenv("pciconfighost") == NULL)
  127. return 1;
  128. #else
  129. return 1;
  130. #endif
  131. }
  132. return 0;
  133. }
  134. /* Get a virtual address associated with a BAR region */
  135. void *pci_map_bar(pci_dev_t pdev, int bar, int flags)
  136. {
  137. pci_addr_t pci_bus_addr;
  138. u32 bar_response;
  139. /* read BAR address */
  140. pci_read_config_dword(pdev, bar, &bar_response);
  141. pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
  142. /*
  143. * Pass "0" as the length argument to pci_bus_to_virt. The arg
  144. * isn't actualy used on any platform because u-boot assumes a static
  145. * linear mapping. In the future, this could read the BAR size
  146. * and pass that as the size if needed.
  147. */
  148. return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE);
  149. }
  150. void pci_write_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum,
  151. u32 addr_and_ctrl)
  152. {
  153. int bar;
  154. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  155. pci_hose_write_config_dword(hose, dev, bar, addr_and_ctrl);
  156. }
  157. u32 pci_read_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum)
  158. {
  159. u32 addr;
  160. int bar;
  161. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  162. pci_hose_read_config_dword(hose, dev, bar, &addr);
  163. if (addr & PCI_BASE_ADDRESS_SPACE_IO)
  164. return addr & PCI_BASE_ADDRESS_IO_MASK;
  165. else
  166. return addr & PCI_BASE_ADDRESS_MEM_MASK;
  167. }
  168. int __pci_hose_bus_to_phys(struct pci_controller *hose,
  169. pci_addr_t bus_addr,
  170. unsigned long flags,
  171. unsigned long skip_mask,
  172. phys_addr_t *pa)
  173. {
  174. struct pci_region *res;
  175. int i;
  176. for (i = 0; i < hose->region_count; i++) {
  177. res = &hose->regions[i];
  178. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  179. continue;
  180. if (res->flags & skip_mask)
  181. continue;
  182. if (bus_addr >= res->bus_start &&
  183. (bus_addr - res->bus_start) < res->size) {
  184. *pa = (bus_addr - res->bus_start + res->phys_start);
  185. return 0;
  186. }
  187. }
  188. return 1;
  189. }
  190. phys_addr_t pci_hose_bus_to_phys(struct pci_controller *hose,
  191. pci_addr_t bus_addr,
  192. unsigned long flags)
  193. {
  194. phys_addr_t phys_addr = 0;
  195. int ret;
  196. if (!hose) {
  197. puts("pci_hose_bus_to_phys: invalid hose\n");
  198. return phys_addr;
  199. }
  200. #ifdef CONFIG_DM_PCI
  201. /* The root controller has the region information */
  202. hose = hose->ctlr->uclass_priv;
  203. #endif
  204. /*
  205. * if PCI_REGION_MEM is set we do a two pass search with preference
  206. * on matches that don't have PCI_REGION_SYS_MEMORY set
  207. */
  208. if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
  209. ret = __pci_hose_bus_to_phys(hose, bus_addr,
  210. flags, PCI_REGION_SYS_MEMORY, &phys_addr);
  211. if (!ret)
  212. return phys_addr;
  213. }
  214. ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
  215. if (ret)
  216. puts("pci_hose_bus_to_phys: invalid physical address\n");
  217. return phys_addr;
  218. }
  219. int __pci_hose_phys_to_bus(struct pci_controller *hose,
  220. phys_addr_t phys_addr,
  221. unsigned long flags,
  222. unsigned long skip_mask,
  223. pci_addr_t *ba)
  224. {
  225. struct pci_region *res;
  226. pci_addr_t bus_addr;
  227. int i;
  228. for (i = 0; i < hose->region_count; i++) {
  229. res = &hose->regions[i];
  230. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  231. continue;
  232. if (res->flags & skip_mask)
  233. continue;
  234. bus_addr = phys_addr - res->phys_start + res->bus_start;
  235. if (bus_addr >= res->bus_start &&
  236. bus_addr < res->bus_start + res->size) {
  237. *ba = bus_addr;
  238. return 0;
  239. }
  240. }
  241. return 1;
  242. }
  243. pci_addr_t pci_hose_phys_to_bus(struct pci_controller *hose,
  244. phys_addr_t phys_addr,
  245. unsigned long flags)
  246. {
  247. pci_addr_t bus_addr = 0;
  248. int ret;
  249. if (!hose) {
  250. puts("pci_hose_phys_to_bus: invalid hose\n");
  251. return bus_addr;
  252. }
  253. /*
  254. * if PCI_REGION_MEM is set we do a two pass search with preference
  255. * on matches that don't have PCI_REGION_SYS_MEMORY set
  256. */
  257. if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
  258. ret = __pci_hose_phys_to_bus(hose, phys_addr,
  259. flags, PCI_REGION_SYS_MEMORY, &bus_addr);
  260. if (!ret)
  261. return bus_addr;
  262. }
  263. ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
  264. if (ret)
  265. puts("pci_hose_phys_to_bus: invalid physical address\n");
  266. return bus_addr;
  267. }
  268. pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
  269. {
  270. struct pci_device_id ids[2] = { {}, {0, 0} };
  271. ids[0].vendor = vendor;
  272. ids[0].device = device;
  273. return pci_find_devices(ids, index);
  274. }
  275. pci_dev_t pci_hose_find_devices(struct pci_controller *hose, int busnum,
  276. struct pci_device_id *ids, int *indexp)
  277. {
  278. int found_multi = 0;
  279. u16 vendor, device;
  280. u8 header_type;
  281. pci_dev_t bdf;
  282. int i;
  283. for (bdf = PCI_BDF(busnum, 0, 0);
  284. bdf < PCI_BDF(busnum + 1, 0, 0);
  285. bdf += PCI_BDF(0, 0, 1)) {
  286. if (pci_skip_dev(hose, bdf))
  287. continue;
  288. if (!PCI_FUNC(bdf)) {
  289. pci_read_config_byte(bdf, PCI_HEADER_TYPE,
  290. &header_type);
  291. found_multi = header_type & 0x80;
  292. } else {
  293. if (!found_multi)
  294. continue;
  295. }
  296. pci_read_config_word(bdf, PCI_VENDOR_ID, &vendor);
  297. pci_read_config_word(bdf, PCI_DEVICE_ID, &device);
  298. for (i = 0; ids[i].vendor != 0; i++) {
  299. if (vendor == ids[i].vendor &&
  300. device == ids[i].device) {
  301. if ((*indexp) <= 0)
  302. return bdf;
  303. (*indexp)--;
  304. }
  305. }
  306. }
  307. return -1;
  308. }