pci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  3. * Andreas Heppel <aheppel@sysgo.de>
  4. *
  5. * (C) Copyright 2002, 2003
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. /*
  11. * PCI routines
  12. */
  13. #include <common.h>
  14. #include <command.h>
  15. #include <errno.h>
  16. #include <asm/processor.h>
  17. #include <asm/io.h>
  18. #include <pci.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. #define PCI_HOSE_OP(rw, size, type) \
  21. int pci_hose_##rw##_config_##size(struct pci_controller *hose, \
  22. pci_dev_t dev, \
  23. int offset, type value) \
  24. { \
  25. return hose->rw##_##size(hose, dev, offset, value); \
  26. }
  27. PCI_HOSE_OP(read, byte, u8 *)
  28. PCI_HOSE_OP(read, word, u16 *)
  29. PCI_HOSE_OP(read, dword, u32 *)
  30. PCI_HOSE_OP(write, byte, u8)
  31. PCI_HOSE_OP(write, word, u16)
  32. PCI_HOSE_OP(write, dword, u32)
  33. #define PCI_OP(rw, size, type, error_code) \
  34. int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \
  35. { \
  36. struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \
  37. \
  38. if (!hose) \
  39. { \
  40. error_code; \
  41. return -1; \
  42. } \
  43. \
  44. return pci_hose_##rw##_config_##size(hose, dev, offset, value); \
  45. }
  46. PCI_OP(read, byte, u8 *, *value = 0xff)
  47. PCI_OP(read, word, u16 *, *value = 0xffff)
  48. PCI_OP(read, dword, u32 *, *value = 0xffffffff)
  49. PCI_OP(write, byte, u8, )
  50. PCI_OP(write, word, u16, )
  51. PCI_OP(write, dword, u32, )
  52. #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \
  53. int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
  54. pci_dev_t dev, \
  55. int offset, type val) \
  56. { \
  57. u32 val32; \
  58. \
  59. if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) { \
  60. *val = -1; \
  61. return -1; \
  62. } \
  63. \
  64. *val = (val32 >> ((offset & (int)off_mask) * 8)); \
  65. \
  66. return 0; \
  67. }
  68. #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \
  69. int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
  70. pci_dev_t dev, \
  71. int offset, type val) \
  72. { \
  73. u32 val32, mask, ldata, shift; \
  74. \
  75. if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
  76. return -1; \
  77. \
  78. shift = ((offset & (int)off_mask) * 8); \
  79. ldata = (((unsigned long)val) & val_mask) << shift; \
  80. mask = val_mask << shift; \
  81. val32 = (val32 & ~mask) | ldata; \
  82. \
  83. if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
  84. return -1; \
  85. \
  86. return 0; \
  87. }
  88. PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
  89. PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
  90. PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
  91. PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
  92. /*
  93. *
  94. */
  95. static struct pci_controller* hose_head;
  96. struct pci_controller *pci_get_hose_head(void)
  97. {
  98. if (gd->hose)
  99. return gd->hose;
  100. return hose_head;
  101. }
  102. void pci_register_hose(struct pci_controller* hose)
  103. {
  104. struct pci_controller **phose = &hose_head;
  105. while(*phose)
  106. phose = &(*phose)->next;
  107. hose->next = NULL;
  108. *phose = hose;
  109. }
  110. struct pci_controller *pci_bus_to_hose(int bus)
  111. {
  112. struct pci_controller *hose;
  113. for (hose = pci_get_hose_head(); hose; hose = hose->next) {
  114. if (bus >= hose->first_busno && bus <= hose->last_busno)
  115. return hose;
  116. }
  117. printf("pci_bus_to_hose() failed\n");
  118. return NULL;
  119. }
  120. struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr)
  121. {
  122. struct pci_controller *hose;
  123. for (hose = pci_get_hose_head(); hose; hose = hose->next) {
  124. if (hose->cfg_addr == cfg_addr)
  125. return hose;
  126. }
  127. return NULL;
  128. }
  129. int pci_last_busno(void)
  130. {
  131. struct pci_controller *hose = pci_get_hose_head();
  132. if (!hose)
  133. return -1;
  134. while (hose->next)
  135. hose = hose->next;
  136. return hose->last_busno;
  137. }
  138. pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
  139. {
  140. struct pci_controller * hose;
  141. pci_dev_t bdf;
  142. int bus;
  143. for (hose = pci_get_hose_head(); hose; hose = hose->next) {
  144. #ifdef CONFIG_SYS_SCSI_SCAN_BUS_REVERSE
  145. for (bus = hose->last_busno; bus >= hose->first_busno; bus--) {
  146. #else
  147. for (bus = hose->first_busno; bus <= hose->last_busno; bus++) {
  148. #endif
  149. bdf = pci_hose_find_devices(hose, bus, ids, &index);
  150. if (bdf != -1)
  151. return bdf;
  152. }
  153. }
  154. return -1;
  155. }
  156. /*
  157. *
  158. */
  159. int __pci_hose_phys_to_bus(struct pci_controller *hose,
  160. phys_addr_t phys_addr,
  161. unsigned long flags,
  162. unsigned long skip_mask,
  163. pci_addr_t *ba)
  164. {
  165. struct pci_region *res;
  166. pci_addr_t bus_addr;
  167. int i;
  168. for (i = 0; i < hose->region_count; i++) {
  169. res = &hose->regions[i];
  170. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  171. continue;
  172. if (res->flags & skip_mask)
  173. continue;
  174. bus_addr = phys_addr - res->phys_start + res->bus_start;
  175. if (bus_addr >= res->bus_start &&
  176. bus_addr < res->bus_start + res->size) {
  177. *ba = bus_addr;
  178. return 0;
  179. }
  180. }
  181. return 1;
  182. }
  183. pci_addr_t pci_hose_phys_to_bus (struct pci_controller *hose,
  184. phys_addr_t phys_addr,
  185. unsigned long flags)
  186. {
  187. pci_addr_t bus_addr = 0;
  188. int ret;
  189. if (!hose) {
  190. puts("pci_hose_phys_to_bus: invalid hose\n");
  191. return bus_addr;
  192. }
  193. /*
  194. * if PCI_REGION_MEM is set we do a two pass search with preference
  195. * on matches that don't have PCI_REGION_SYS_MEMORY set
  196. */
  197. if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
  198. ret = __pci_hose_phys_to_bus(hose, phys_addr,
  199. flags, PCI_REGION_SYS_MEMORY, &bus_addr);
  200. if (!ret)
  201. return bus_addr;
  202. }
  203. ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
  204. if (ret)
  205. puts("pci_hose_phys_to_bus: invalid physical address\n");
  206. return bus_addr;
  207. }
  208. int pci_hose_config_device(struct pci_controller *hose,
  209. pci_dev_t dev,
  210. unsigned long io,
  211. pci_addr_t mem,
  212. unsigned long command)
  213. {
  214. u32 bar_response;
  215. unsigned int old_command;
  216. pci_addr_t bar_value;
  217. pci_size_t bar_size;
  218. unsigned char pin;
  219. int bar, found_mem64;
  220. debug("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n", io,
  221. (u64)mem, command);
  222. pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 0);
  223. for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_5; bar += 4) {
  224. pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
  225. pci_hose_read_config_dword(hose, dev, bar, &bar_response);
  226. if (!bar_response)
  227. continue;
  228. found_mem64 = 0;
  229. /* Check the BAR type and set our address mask */
  230. if (bar_response & PCI_BASE_ADDRESS_SPACE) {
  231. bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
  232. /* round up region base address to a multiple of size */
  233. io = ((io - 1) | (bar_size - 1)) + 1;
  234. bar_value = io;
  235. /* compute new region base address */
  236. io = io + bar_size;
  237. } else {
  238. if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  239. PCI_BASE_ADDRESS_MEM_TYPE_64) {
  240. u32 bar_response_upper;
  241. u64 bar64;
  242. pci_hose_write_config_dword(hose, dev, bar + 4,
  243. 0xffffffff);
  244. pci_hose_read_config_dword(hose, dev, bar + 4,
  245. &bar_response_upper);
  246. bar64 = ((u64)bar_response_upper << 32) | bar_response;
  247. bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
  248. found_mem64 = 1;
  249. } else {
  250. bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
  251. }
  252. /* round up region base address to multiple of size */
  253. mem = ((mem - 1) | (bar_size - 1)) + 1;
  254. bar_value = mem;
  255. /* compute new region base address */
  256. mem = mem + bar_size;
  257. }
  258. /* Write it out and update our limit */
  259. pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value);
  260. if (found_mem64) {
  261. bar += 4;
  262. #ifdef CONFIG_SYS_PCI_64BIT
  263. pci_hose_write_config_dword(hose, dev, bar,
  264. (u32)(bar_value >> 32));
  265. #else
  266. pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
  267. #endif
  268. }
  269. }
  270. /* Configure Cache Line Size Register */
  271. pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
  272. /* Configure Latency Timer */
  273. pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
  274. /* Disable interrupt line, if device says it wants to use interrupts */
  275. pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_PIN, &pin);
  276. if (pin != 0) {
  277. pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE, 0xff);
  278. }
  279. pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &old_command);
  280. pci_hose_write_config_dword(hose, dev, PCI_COMMAND,
  281. (old_command & 0xffff0000) | command);
  282. return 0;
  283. }
  284. /*
  285. *
  286. */
  287. struct pci_config_table *pci_find_config(struct pci_controller *hose,
  288. unsigned short class,
  289. unsigned int vendor,
  290. unsigned int device,
  291. unsigned int bus,
  292. unsigned int dev,
  293. unsigned int func)
  294. {
  295. struct pci_config_table *table;
  296. for (table = hose->config_table; table && table->vendor; table++) {
  297. if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
  298. (table->device == PCI_ANY_ID || table->device == device) &&
  299. (table->class == PCI_ANY_ID || table->class == class) &&
  300. (table->bus == PCI_ANY_ID || table->bus == bus) &&
  301. (table->dev == PCI_ANY_ID || table->dev == dev) &&
  302. (table->func == PCI_ANY_ID || table->func == func)) {
  303. return table;
  304. }
  305. }
  306. return NULL;
  307. }
  308. void pci_cfgfunc_config_device(struct pci_controller *hose,
  309. pci_dev_t dev,
  310. struct pci_config_table *entry)
  311. {
  312. pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1],
  313. entry->priv[2]);
  314. }
  315. void pci_cfgfunc_do_nothing(struct pci_controller *hose,
  316. pci_dev_t dev, struct pci_config_table *entry)
  317. {
  318. }
  319. /*
  320. * HJF: Changed this to return int. I think this is required
  321. * to get the correct result when scanning bridges
  322. */
  323. extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
  324. #ifdef CONFIG_PCI_SCAN_SHOW
  325. __weak int pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
  326. {
  327. if (dev == PCI_BDF(hose->first_busno, 0, 0))
  328. return 0;
  329. return 1;
  330. }
  331. #endif /* CONFIG_PCI_SCAN_SHOW */
  332. int pci_hose_scan_bus(struct pci_controller *hose, int bus)
  333. {
  334. unsigned int sub_bus, found_multi = 0;
  335. unsigned short vendor, device, class;
  336. unsigned char header_type;
  337. #ifndef CONFIG_PCI_PNP
  338. struct pci_config_table *cfg;
  339. #endif
  340. pci_dev_t dev;
  341. #ifdef CONFIG_PCI_SCAN_SHOW
  342. static int indent = 0;
  343. #endif
  344. sub_bus = bus;
  345. for (dev = PCI_BDF(bus,0,0);
  346. dev < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
  347. PCI_MAX_PCI_FUNCTIONS - 1);
  348. dev += PCI_BDF(0, 0, 1)) {
  349. if (pci_skip_dev(hose, dev))
  350. continue;
  351. if (PCI_FUNC(dev) && !found_multi)
  352. continue;
  353. pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
  354. pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
  355. if (vendor == 0xffff || vendor == 0x0000)
  356. continue;
  357. if (!PCI_FUNC(dev))
  358. found_multi = header_type & 0x80;
  359. debug("PCI Scan: Found Bus %d, Device %d, Function %d\n",
  360. PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
  361. pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
  362. pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
  363. #ifdef CONFIG_PCI_FIXUP_DEV
  364. board_pci_fixup_dev(hose, dev, vendor, device, class);
  365. #endif
  366. #ifdef CONFIG_PCI_SCAN_SHOW
  367. indent++;
  368. /* Print leading space, including bus indentation */
  369. printf("%*c", indent + 1, ' ');
  370. if (pci_print_dev(hose, dev)) {
  371. printf("%02x:%02x.%-*x - %04x:%04x - %s\n",
  372. PCI_BUS(dev), PCI_DEV(dev), 6 - indent, PCI_FUNC(dev),
  373. vendor, device, pci_class_str(class >> 8));
  374. }
  375. #endif
  376. #ifdef CONFIG_PCI_PNP
  377. sub_bus = max((unsigned int)pciauto_config_device(hose, dev),
  378. sub_bus);
  379. #else
  380. cfg = pci_find_config(hose, class, vendor, device,
  381. PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
  382. if (cfg) {
  383. cfg->config_device(hose, dev, cfg);
  384. sub_bus = max(sub_bus,
  385. (unsigned int)hose->current_busno);
  386. }
  387. #endif
  388. #ifdef CONFIG_PCI_SCAN_SHOW
  389. indent--;
  390. #endif
  391. if (hose->fixup_irq)
  392. hose->fixup_irq(hose, dev);
  393. }
  394. return sub_bus;
  395. }
  396. int pci_hose_scan(struct pci_controller *hose)
  397. {
  398. #if defined(CONFIG_PCI_BOOTDELAY)
  399. char *s;
  400. int i;
  401. if (!gd->pcidelay_done) {
  402. /* wait "pcidelay" ms (if defined)... */
  403. s = getenv("pcidelay");
  404. if (s) {
  405. int val = simple_strtoul(s, NULL, 10);
  406. for (i = 0; i < val; i++)
  407. udelay(1000);
  408. }
  409. gd->pcidelay_done = 1;
  410. }
  411. #endif /* CONFIG_PCI_BOOTDELAY */
  412. /*
  413. * Start scan at current_busno.
  414. * PCIe will start scan at first_busno+1.
  415. */
  416. /* For legacy support, ensure current >= first */
  417. if (hose->first_busno > hose->current_busno)
  418. hose->current_busno = hose->first_busno;
  419. #ifdef CONFIG_PCI_PNP
  420. pciauto_config_init(hose);
  421. #endif
  422. return pci_hose_scan_bus(hose, hose->current_busno);
  423. }
  424. void pci_init(void)
  425. {
  426. hose_head = NULL;
  427. /* now call board specific pci_init()... */
  428. pci_init_board();
  429. }
  430. /* Returns the address of the requested capability structure within the
  431. * device's PCI configuration space or 0 in case the device does not
  432. * support it.
  433. * */
  434. int pci_hose_find_capability(struct pci_controller *hose, pci_dev_t dev,
  435. int cap)
  436. {
  437. int pos;
  438. u8 hdr_type;
  439. pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &hdr_type);
  440. pos = pci_hose_find_cap_start(hose, dev, hdr_type & 0x7F);
  441. if (pos)
  442. pos = pci_find_cap(hose, dev, pos, cap);
  443. return pos;
  444. }
  445. /* Find the header pointer to the Capabilities*/
  446. int pci_hose_find_cap_start(struct pci_controller *hose, pci_dev_t dev,
  447. u8 hdr_type)
  448. {
  449. u16 status;
  450. pci_hose_read_config_word(hose, dev, PCI_STATUS, &status);
  451. if (!(status & PCI_STATUS_CAP_LIST))
  452. return 0;
  453. switch (hdr_type) {
  454. case PCI_HEADER_TYPE_NORMAL:
  455. case PCI_HEADER_TYPE_BRIDGE:
  456. return PCI_CAPABILITY_LIST;
  457. case PCI_HEADER_TYPE_CARDBUS:
  458. return PCI_CB_CAPABILITY_LIST;
  459. default:
  460. return 0;
  461. }
  462. }
  463. int pci_find_cap(struct pci_controller *hose, pci_dev_t dev, int pos, int cap)
  464. {
  465. int ttl = PCI_FIND_CAP_TTL;
  466. u8 id;
  467. u8 next_pos;
  468. while (ttl--) {
  469. pci_hose_read_config_byte(hose, dev, pos, &next_pos);
  470. if (next_pos < CAP_START_POS)
  471. break;
  472. next_pos &= ~3;
  473. pos = (int) next_pos;
  474. pci_hose_read_config_byte(hose, dev,
  475. pos + PCI_CAP_LIST_ID, &id);
  476. if (id == 0xff)
  477. break;
  478. if (id == cap)
  479. return pos;
  480. pos += PCI_CAP_LIST_NEXT;
  481. }
  482. return 0;
  483. }