pci_common.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. __weak int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
  80. {
  81. /*
  82. * Check if pci device should be skipped in configuration
  83. */
  84. if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
  85. #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
  86. /*
  87. * Only skip configuration if "pciconfighost" is not set
  88. */
  89. if (getenv("pciconfighost") == NULL)
  90. return 1;
  91. #else
  92. return 1;
  93. #endif
  94. }
  95. return 0;
  96. }
  97. #if !defined(CONFIG_DM_PCI) || defined(CONFIG_DM_PCI_COMPAT)
  98. /* Get a virtual address associated with a BAR region */
  99. void *pci_map_bar(pci_dev_t pdev, int bar, int flags)
  100. {
  101. pci_addr_t pci_bus_addr;
  102. u32 bar_response;
  103. /* read BAR address */
  104. pci_read_config_dword(pdev, bar, &bar_response);
  105. pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
  106. /*
  107. * Pass "0" as the length argument to pci_bus_to_virt. The arg
  108. * isn't actualy used on any platform because u-boot assumes a static
  109. * linear mapping. In the future, this could read the BAR size
  110. * and pass that as the size if needed.
  111. */
  112. return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE);
  113. }
  114. void pci_write_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum,
  115. u32 addr_and_ctrl)
  116. {
  117. int bar;
  118. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  119. pci_hose_write_config_dword(hose, dev, bar, addr_and_ctrl);
  120. }
  121. u32 pci_read_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum)
  122. {
  123. u32 addr;
  124. int bar;
  125. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  126. pci_hose_read_config_dword(hose, dev, bar, &addr);
  127. if (addr & PCI_BASE_ADDRESS_SPACE_IO)
  128. return addr & PCI_BASE_ADDRESS_IO_MASK;
  129. else
  130. return addr & PCI_BASE_ADDRESS_MEM_MASK;
  131. }
  132. int __pci_hose_bus_to_phys(struct pci_controller *hose,
  133. pci_addr_t bus_addr,
  134. unsigned long flags,
  135. unsigned long skip_mask,
  136. phys_addr_t *pa)
  137. {
  138. struct pci_region *res;
  139. int i;
  140. for (i = 0; i < hose->region_count; i++) {
  141. res = &hose->regions[i];
  142. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  143. continue;
  144. if (res->flags & skip_mask)
  145. continue;
  146. if (bus_addr >= res->bus_start &&
  147. (bus_addr - res->bus_start) < res->size) {
  148. *pa = (bus_addr - res->bus_start + res->phys_start);
  149. return 0;
  150. }
  151. }
  152. return 1;
  153. }
  154. phys_addr_t pci_hose_bus_to_phys(struct pci_controller *hose,
  155. pci_addr_t bus_addr,
  156. unsigned long flags)
  157. {
  158. phys_addr_t phys_addr = 0;
  159. int ret;
  160. if (!hose) {
  161. puts("pci_hose_bus_to_phys: invalid hose\n");
  162. return phys_addr;
  163. }
  164. #ifdef CONFIG_DM_PCI
  165. /* The root controller has the region information */
  166. hose = pci_bus_to_hose(0);
  167. #endif
  168. /*
  169. * if PCI_REGION_MEM is set we do a two pass search with preference
  170. * on matches that don't have PCI_REGION_SYS_MEMORY set
  171. */
  172. if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
  173. ret = __pci_hose_bus_to_phys(hose, bus_addr,
  174. flags, PCI_REGION_SYS_MEMORY, &phys_addr);
  175. if (!ret)
  176. return phys_addr;
  177. }
  178. ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
  179. if (ret)
  180. puts("pci_hose_bus_to_phys: invalid physical address\n");
  181. return phys_addr;
  182. }
  183. int __pci_hose_phys_to_bus(struct pci_controller *hose,
  184. phys_addr_t phys_addr,
  185. unsigned long flags,
  186. unsigned long skip_mask,
  187. pci_addr_t *ba)
  188. {
  189. struct pci_region *res;
  190. pci_addr_t bus_addr;
  191. int i;
  192. for (i = 0; i < hose->region_count; i++) {
  193. res = &hose->regions[i];
  194. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  195. continue;
  196. if (res->flags & skip_mask)
  197. continue;
  198. bus_addr = phys_addr - res->phys_start + res->bus_start;
  199. if (bus_addr >= res->bus_start &&
  200. (bus_addr - res->bus_start) < res->size) {
  201. *ba = bus_addr;
  202. return 0;
  203. }
  204. }
  205. return 1;
  206. }
  207. pci_addr_t pci_hose_phys_to_bus(struct pci_controller *hose,
  208. phys_addr_t phys_addr,
  209. unsigned long flags)
  210. {
  211. pci_addr_t bus_addr = 0;
  212. int ret;
  213. if (!hose) {
  214. puts("pci_hose_phys_to_bus: invalid hose\n");
  215. return bus_addr;
  216. }
  217. #ifdef CONFIG_DM_PCI
  218. /* The root controller has the region information */
  219. hose = pci_bus_to_hose(0);
  220. #endif
  221. /*
  222. * if PCI_REGION_MEM is set we do a two pass search with preference
  223. * on matches that don't have PCI_REGION_SYS_MEMORY set
  224. */
  225. if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
  226. ret = __pci_hose_phys_to_bus(hose, phys_addr,
  227. flags, PCI_REGION_SYS_MEMORY, &bus_addr);
  228. if (!ret)
  229. return bus_addr;
  230. }
  231. ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
  232. if (ret)
  233. puts("pci_hose_phys_to_bus: invalid physical address\n");
  234. return bus_addr;
  235. }
  236. pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
  237. {
  238. struct pci_device_id ids[2] = { {}, {0, 0} };
  239. ids[0].vendor = vendor;
  240. ids[0].device = device;
  241. return pci_find_devices(ids, index);
  242. }
  243. pci_dev_t pci_hose_find_devices(struct pci_controller *hose, int busnum,
  244. struct pci_device_id *ids, int *indexp)
  245. {
  246. int found_multi = 0;
  247. u16 vendor, device;
  248. u8 header_type;
  249. pci_dev_t bdf;
  250. int i;
  251. for (bdf = PCI_BDF(busnum, 0, 0);
  252. bdf < PCI_BDF(busnum + 1, 0, 0);
  253. bdf += PCI_BDF(0, 0, 1)) {
  254. if (pci_skip_dev(hose, bdf))
  255. continue;
  256. if (!PCI_FUNC(bdf)) {
  257. pci_read_config_byte(bdf, PCI_HEADER_TYPE,
  258. &header_type);
  259. found_multi = header_type & 0x80;
  260. } else {
  261. if (!found_multi)
  262. continue;
  263. }
  264. pci_read_config_word(bdf, PCI_VENDOR_ID, &vendor);
  265. pci_read_config_word(bdf, PCI_DEVICE_ID, &device);
  266. for (i = 0; ids[i].vendor != 0; i++) {
  267. if (vendor == ids[i].vendor &&
  268. device == ids[i].device) {
  269. if ((*indexp) <= 0)
  270. return bdf;
  271. (*indexp)--;
  272. }
  273. }
  274. }
  275. return -1;
  276. }
  277. pci_dev_t pci_find_class(uint find_class, int index)
  278. {
  279. int bus;
  280. int devnum;
  281. pci_dev_t bdf;
  282. uint32_t class;
  283. for (bus = 0; bus <= pci_last_busno(); bus++) {
  284. for (devnum = 0; devnum < PCI_MAX_PCI_DEVICES - 1; devnum++) {
  285. pci_read_config_dword(PCI_BDF(bus, devnum, 0),
  286. PCI_CLASS_REVISION, &class);
  287. if (class >> 16 == 0xffff)
  288. continue;
  289. for (bdf = PCI_BDF(bus, devnum, 0);
  290. bdf <= PCI_BDF(bus, devnum,
  291. PCI_MAX_PCI_FUNCTIONS - 1);
  292. bdf += PCI_BDF(0, 0, 1)) {
  293. pci_read_config_dword(bdf, PCI_CLASS_REVISION,
  294. &class);
  295. class >>= 8;
  296. if (class != find_class)
  297. continue;
  298. /*
  299. * Decrement the index. We want to return the
  300. * correct device, so index is 0 for the first
  301. * matching device, 1 for the second, etc.
  302. */
  303. if (index) {
  304. index--;
  305. continue;
  306. }
  307. /* Return index'th controller. */
  308. return bdf;
  309. }
  310. }
  311. }
  312. return -ENODEV;
  313. }
  314. #endif /* !CONFIG_DM_PCI || CONFIG_DM_PCI_COMPAT */