pci.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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 <asm/processor.h>
  16. #include <asm/io.h>
  17. #include <pci.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. #define PCI_HOSE_OP(rw, size, type) \
  20. int pci_hose_##rw##_config_##size(struct pci_controller *hose, \
  21. pci_dev_t dev, \
  22. int offset, type value) \
  23. { \
  24. return hose->rw##_##size(hose, dev, offset, value); \
  25. }
  26. PCI_HOSE_OP(read, byte, u8 *)
  27. PCI_HOSE_OP(read, word, u16 *)
  28. PCI_HOSE_OP(read, dword, u32 *)
  29. PCI_HOSE_OP(write, byte, u8)
  30. PCI_HOSE_OP(write, word, u16)
  31. PCI_HOSE_OP(write, dword, u32)
  32. #define PCI_OP(rw, size, type, error_code) \
  33. int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \
  34. { \
  35. struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \
  36. \
  37. if (!hose) \
  38. { \
  39. error_code; \
  40. return -1; \
  41. } \
  42. \
  43. return pci_hose_##rw##_config_##size(hose, dev, offset, value); \
  44. }
  45. PCI_OP(read, byte, u8 *, *value = 0xff)
  46. PCI_OP(read, word, u16 *, *value = 0xffff)
  47. PCI_OP(read, dword, u32 *, *value = 0xffffffff)
  48. PCI_OP(write, byte, u8, )
  49. PCI_OP(write, word, u16, )
  50. PCI_OP(write, dword, u32, )
  51. #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \
  52. int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
  53. pci_dev_t dev, \
  54. int offset, type val) \
  55. { \
  56. u32 val32; \
  57. \
  58. if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) { \
  59. *val = -1; \
  60. return -1; \
  61. } \
  62. \
  63. *val = (val32 >> ((offset & (int)off_mask) * 8)); \
  64. \
  65. return 0; \
  66. }
  67. #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \
  68. int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
  69. pci_dev_t dev, \
  70. int offset, type val) \
  71. { \
  72. u32 val32, mask, ldata, shift; \
  73. \
  74. if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
  75. return -1; \
  76. \
  77. shift = ((offset & (int)off_mask) * 8); \
  78. ldata = (((unsigned long)val) & val_mask) << shift; \
  79. mask = val_mask << shift; \
  80. val32 = (val32 & ~mask) | ldata; \
  81. \
  82. if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
  83. return -1; \
  84. \
  85. return 0; \
  86. }
  87. PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
  88. PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
  89. PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
  90. PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
  91. /* Get a virtual address associated with a BAR region */
  92. void *pci_map_bar(pci_dev_t pdev, int bar, int flags)
  93. {
  94. pci_addr_t pci_bus_addr;
  95. u32 bar_response;
  96. /* read BAR address */
  97. pci_read_config_dword(pdev, bar, &bar_response);
  98. pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
  99. /*
  100. * Pass "0" as the length argument to pci_bus_to_virt. The arg
  101. * isn't actualy used on any platform because u-boot assumes a static
  102. * linear mapping. In the future, this could read the BAR size
  103. * and pass that as the size if needed.
  104. */
  105. return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE);
  106. }
  107. /*
  108. *
  109. */
  110. static struct pci_controller* hose_head;
  111. struct pci_controller *pci_get_hose_head(void)
  112. {
  113. if (gd->hose)
  114. return gd->hose;
  115. return hose_head;
  116. }
  117. void pci_register_hose(struct pci_controller* hose)
  118. {
  119. struct pci_controller **phose = &hose_head;
  120. while(*phose)
  121. phose = &(*phose)->next;
  122. hose->next = NULL;
  123. *phose = hose;
  124. }
  125. struct pci_controller *pci_bus_to_hose(int bus)
  126. {
  127. struct pci_controller *hose;
  128. for (hose = pci_get_hose_head(); hose; hose = hose->next) {
  129. if (bus >= hose->first_busno && bus <= hose->last_busno)
  130. return hose;
  131. }
  132. printf("pci_bus_to_hose() failed\n");
  133. return NULL;
  134. }
  135. struct pci_controller *find_hose_by_cfg_addr(void *cfg_addr)
  136. {
  137. struct pci_controller *hose;
  138. for (hose = pci_get_hose_head(); hose; hose = hose->next) {
  139. if (hose->cfg_addr == cfg_addr)
  140. return hose;
  141. }
  142. return NULL;
  143. }
  144. int pci_last_busno(void)
  145. {
  146. struct pci_controller *hose = pci_get_hose_head();
  147. if (!hose)
  148. return -1;
  149. while (hose->next)
  150. hose = hose->next;
  151. return hose->last_busno;
  152. }
  153. pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
  154. {
  155. struct pci_controller * hose;
  156. u16 vendor, device;
  157. u8 header_type;
  158. pci_dev_t bdf;
  159. int i, bus, found_multi = 0;
  160. for (hose = pci_get_hose_head(); hose; hose = hose->next) {
  161. #ifdef CONFIG_SYS_SCSI_SCAN_BUS_REVERSE
  162. for (bus = hose->last_busno; bus >= hose->first_busno; bus--)
  163. #else
  164. for (bus = hose->first_busno; bus <= hose->last_busno; bus++)
  165. #endif
  166. for (bdf = PCI_BDF(bus, 0, 0);
  167. #if defined(CONFIG_ELPPC) || defined(CONFIG_PPMC7XX)
  168. bdf < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
  169. PCI_MAX_PCI_FUNCTIONS - 1);
  170. #else
  171. bdf < PCI_BDF(bus + 1, 0, 0);
  172. #endif
  173. bdf += PCI_BDF(0, 0, 1)) {
  174. if (pci_skip_dev(hose, bdf))
  175. continue;
  176. if (!PCI_FUNC(bdf)) {
  177. pci_read_config_byte(bdf,
  178. PCI_HEADER_TYPE,
  179. &header_type);
  180. found_multi = header_type & 0x80;
  181. } else {
  182. if (!found_multi)
  183. continue;
  184. }
  185. pci_read_config_word(bdf,
  186. PCI_VENDOR_ID,
  187. &vendor);
  188. pci_read_config_word(bdf,
  189. PCI_DEVICE_ID,
  190. &device);
  191. for (i = 0; ids[i].vendor != 0; i++) {
  192. if (vendor == ids[i].vendor &&
  193. device == ids[i].device) {
  194. if (index <= 0)
  195. return bdf;
  196. index--;
  197. }
  198. }
  199. }
  200. }
  201. return -1;
  202. }
  203. pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
  204. {
  205. struct pci_device_id ids[2] = { {}, {0, 0} };
  206. ids[0].vendor = vendor;
  207. ids[0].device = device;
  208. return pci_find_devices(ids, index);
  209. }
  210. /*
  211. *
  212. */
  213. int __pci_hose_phys_to_bus(struct pci_controller *hose,
  214. phys_addr_t phys_addr,
  215. unsigned long flags,
  216. unsigned long skip_mask,
  217. pci_addr_t *ba)
  218. {
  219. struct pci_region *res;
  220. pci_addr_t bus_addr;
  221. int i;
  222. for (i = 0; i < hose->region_count; i++) {
  223. res = &hose->regions[i];
  224. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  225. continue;
  226. if (res->flags & skip_mask)
  227. continue;
  228. bus_addr = phys_addr - res->phys_start + res->bus_start;
  229. if (bus_addr >= res->bus_start &&
  230. bus_addr < res->bus_start + res->size) {
  231. *ba = bus_addr;
  232. return 0;
  233. }
  234. }
  235. return 1;
  236. }
  237. pci_addr_t pci_hose_phys_to_bus (struct pci_controller *hose,
  238. phys_addr_t phys_addr,
  239. unsigned long flags)
  240. {
  241. pci_addr_t bus_addr = 0;
  242. int ret;
  243. if (!hose) {
  244. puts("pci_hose_phys_to_bus: invalid hose\n");
  245. return bus_addr;
  246. }
  247. /*
  248. * if PCI_REGION_MEM is set we do a two pass search with preference
  249. * on matches that don't have PCI_REGION_SYS_MEMORY set
  250. */
  251. if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
  252. ret = __pci_hose_phys_to_bus(hose, phys_addr,
  253. flags, PCI_REGION_SYS_MEMORY, &bus_addr);
  254. if (!ret)
  255. return bus_addr;
  256. }
  257. ret = __pci_hose_phys_to_bus(hose, phys_addr, flags, 0, &bus_addr);
  258. if (ret)
  259. puts("pci_hose_phys_to_bus: invalid physical address\n");
  260. return bus_addr;
  261. }
  262. int __pci_hose_bus_to_phys(struct pci_controller *hose,
  263. pci_addr_t bus_addr,
  264. unsigned long flags,
  265. unsigned long skip_mask,
  266. phys_addr_t *pa)
  267. {
  268. struct pci_region *res;
  269. int i;
  270. for (i = 0; i < hose->region_count; i++) {
  271. res = &hose->regions[i];
  272. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  273. continue;
  274. if (res->flags & skip_mask)
  275. continue;
  276. if (bus_addr >= res->bus_start &&
  277. (bus_addr - res->bus_start) < res->size) {
  278. *pa = (bus_addr - res->bus_start + res->phys_start);
  279. return 0;
  280. }
  281. }
  282. return 1;
  283. }
  284. phys_addr_t pci_hose_bus_to_phys(struct pci_controller* hose,
  285. pci_addr_t bus_addr,
  286. unsigned long flags)
  287. {
  288. phys_addr_t phys_addr = 0;
  289. int ret;
  290. if (!hose) {
  291. puts("pci_hose_bus_to_phys: invalid hose\n");
  292. return phys_addr;
  293. }
  294. /*
  295. * if PCI_REGION_MEM is set we do a two pass search with preference
  296. * on matches that don't have PCI_REGION_SYS_MEMORY set
  297. */
  298. if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
  299. ret = __pci_hose_bus_to_phys(hose, bus_addr,
  300. flags, PCI_REGION_SYS_MEMORY, &phys_addr);
  301. if (!ret)
  302. return phys_addr;
  303. }
  304. ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
  305. if (ret)
  306. puts("pci_hose_bus_to_phys: invalid physical address\n");
  307. return phys_addr;
  308. }
  309. void pci_write_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum,
  310. u32 addr_and_ctrl)
  311. {
  312. int bar;
  313. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  314. pci_hose_write_config_dword(hose, dev, bar, addr_and_ctrl);
  315. }
  316. u32 pci_read_bar32(struct pci_controller *hose, pci_dev_t dev, int barnum)
  317. {
  318. u32 addr;
  319. int bar;
  320. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  321. pci_hose_read_config_dword(hose, dev, bar, &addr);
  322. if (addr & PCI_BASE_ADDRESS_SPACE_IO)
  323. return addr & PCI_BASE_ADDRESS_IO_MASK;
  324. else
  325. return addr & PCI_BASE_ADDRESS_MEM_MASK;
  326. }
  327. int pci_hose_config_device(struct pci_controller *hose,
  328. pci_dev_t dev,
  329. unsigned long io,
  330. pci_addr_t mem,
  331. unsigned long command)
  332. {
  333. u32 bar_response;
  334. unsigned int old_command;
  335. pci_addr_t bar_value;
  336. pci_size_t bar_size;
  337. unsigned char pin;
  338. int bar, found_mem64;
  339. debug("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n", io,
  340. (u64)mem, command);
  341. pci_hose_write_config_dword(hose, dev, PCI_COMMAND, 0);
  342. for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_5; bar += 4) {
  343. pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
  344. pci_hose_read_config_dword(hose, dev, bar, &bar_response);
  345. if (!bar_response)
  346. continue;
  347. found_mem64 = 0;
  348. /* Check the BAR type and set our address mask */
  349. if (bar_response & PCI_BASE_ADDRESS_SPACE) {
  350. bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
  351. /* round up region base address to a multiple of size */
  352. io = ((io - 1) | (bar_size - 1)) + 1;
  353. bar_value = io;
  354. /* compute new region base address */
  355. io = io + bar_size;
  356. } else {
  357. if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  358. PCI_BASE_ADDRESS_MEM_TYPE_64) {
  359. u32 bar_response_upper;
  360. u64 bar64;
  361. pci_hose_write_config_dword(hose, dev, bar + 4,
  362. 0xffffffff);
  363. pci_hose_read_config_dword(hose, dev, bar + 4,
  364. &bar_response_upper);
  365. bar64 = ((u64)bar_response_upper << 32) | bar_response;
  366. bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
  367. found_mem64 = 1;
  368. } else {
  369. bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
  370. }
  371. /* round up region base address to multiple of size */
  372. mem = ((mem - 1) | (bar_size - 1)) + 1;
  373. bar_value = mem;
  374. /* compute new region base address */
  375. mem = mem + bar_size;
  376. }
  377. /* Write it out and update our limit */
  378. pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value);
  379. if (found_mem64) {
  380. bar += 4;
  381. #ifdef CONFIG_SYS_PCI_64BIT
  382. pci_hose_write_config_dword(hose, dev, bar,
  383. (u32)(bar_value >> 32));
  384. #else
  385. pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
  386. #endif
  387. }
  388. }
  389. /* Configure Cache Line Size Register */
  390. pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
  391. /* Configure Latency Timer */
  392. pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
  393. /* Disable interrupt line, if device says it wants to use interrupts */
  394. pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_PIN, &pin);
  395. if (pin != 0) {
  396. pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE, 0xff);
  397. }
  398. pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &old_command);
  399. pci_hose_write_config_dword(hose, dev, PCI_COMMAND,
  400. (old_command & 0xffff0000) | command);
  401. return 0;
  402. }
  403. /*
  404. *
  405. */
  406. struct pci_config_table *pci_find_config(struct pci_controller *hose,
  407. unsigned short class,
  408. unsigned int vendor,
  409. unsigned int device,
  410. unsigned int bus,
  411. unsigned int dev,
  412. unsigned int func)
  413. {
  414. struct pci_config_table *table;
  415. for (table = hose->config_table; table && table->vendor; table++) {
  416. if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
  417. (table->device == PCI_ANY_ID || table->device == device) &&
  418. (table->class == PCI_ANY_ID || table->class == class) &&
  419. (table->bus == PCI_ANY_ID || table->bus == bus) &&
  420. (table->dev == PCI_ANY_ID || table->dev == dev) &&
  421. (table->func == PCI_ANY_ID || table->func == func)) {
  422. return table;
  423. }
  424. }
  425. return NULL;
  426. }
  427. void pci_cfgfunc_config_device(struct pci_controller *hose,
  428. pci_dev_t dev,
  429. struct pci_config_table *entry)
  430. {
  431. pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1],
  432. entry->priv[2]);
  433. }
  434. void pci_cfgfunc_do_nothing(struct pci_controller *hose,
  435. pci_dev_t dev, struct pci_config_table *entry)
  436. {
  437. }
  438. /*
  439. * HJF: Changed this to return int. I think this is required
  440. * to get the correct result when scanning bridges
  441. */
  442. extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
  443. #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI_SCAN_SHOW)
  444. const char * pci_class_str(u8 class)
  445. {
  446. switch (class) {
  447. case PCI_CLASS_NOT_DEFINED:
  448. return "Build before PCI Rev2.0";
  449. break;
  450. case PCI_BASE_CLASS_STORAGE:
  451. return "Mass storage controller";
  452. break;
  453. case PCI_BASE_CLASS_NETWORK:
  454. return "Network controller";
  455. break;
  456. case PCI_BASE_CLASS_DISPLAY:
  457. return "Display controller";
  458. break;
  459. case PCI_BASE_CLASS_MULTIMEDIA:
  460. return "Multimedia device";
  461. break;
  462. case PCI_BASE_CLASS_MEMORY:
  463. return "Memory controller";
  464. break;
  465. case PCI_BASE_CLASS_BRIDGE:
  466. return "Bridge device";
  467. break;
  468. case PCI_BASE_CLASS_COMMUNICATION:
  469. return "Simple comm. controller";
  470. break;
  471. case PCI_BASE_CLASS_SYSTEM:
  472. return "Base system peripheral";
  473. break;
  474. case PCI_BASE_CLASS_INPUT:
  475. return "Input device";
  476. break;
  477. case PCI_BASE_CLASS_DOCKING:
  478. return "Docking station";
  479. break;
  480. case PCI_BASE_CLASS_PROCESSOR:
  481. return "Processor";
  482. break;
  483. case PCI_BASE_CLASS_SERIAL:
  484. return "Serial bus controller";
  485. break;
  486. case PCI_BASE_CLASS_INTELLIGENT:
  487. return "Intelligent controller";
  488. break;
  489. case PCI_BASE_CLASS_SATELLITE:
  490. return "Satellite controller";
  491. break;
  492. case PCI_BASE_CLASS_CRYPT:
  493. return "Cryptographic device";
  494. break;
  495. case PCI_BASE_CLASS_SIGNAL_PROCESSING:
  496. return "DSP";
  497. break;
  498. case PCI_CLASS_OTHERS:
  499. return "Does not fit any class";
  500. break;
  501. default:
  502. return "???";
  503. break;
  504. };
  505. }
  506. #endif /* CONFIG_CMD_PCI || CONFIG_PCI_SCAN_SHOW */
  507. __weak int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
  508. {
  509. /*
  510. * Check if pci device should be skipped in configuration
  511. */
  512. if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
  513. #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
  514. /*
  515. * Only skip configuration if "pciconfighost" is not set
  516. */
  517. if (getenv("pciconfighost") == NULL)
  518. return 1;
  519. #else
  520. return 1;
  521. #endif
  522. }
  523. return 0;
  524. }
  525. #ifdef CONFIG_PCI_SCAN_SHOW
  526. __weak int pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
  527. {
  528. if (dev == PCI_BDF(hose->first_busno, 0, 0))
  529. return 0;
  530. return 1;
  531. }
  532. #endif /* CONFIG_PCI_SCAN_SHOW */
  533. int pci_hose_scan_bus(struct pci_controller *hose, int bus)
  534. {
  535. unsigned int sub_bus, found_multi = 0;
  536. unsigned short vendor, device, class;
  537. unsigned char header_type;
  538. #ifndef CONFIG_PCI_PNP
  539. struct pci_config_table *cfg;
  540. #endif
  541. pci_dev_t dev;
  542. #ifdef CONFIG_PCI_SCAN_SHOW
  543. static int indent = 0;
  544. #endif
  545. sub_bus = bus;
  546. for (dev = PCI_BDF(bus,0,0);
  547. dev < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
  548. PCI_MAX_PCI_FUNCTIONS - 1);
  549. dev += PCI_BDF(0, 0, 1)) {
  550. if (pci_skip_dev(hose, dev))
  551. continue;
  552. if (PCI_FUNC(dev) && !found_multi)
  553. continue;
  554. pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
  555. pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
  556. if (vendor == 0xffff || vendor == 0x0000)
  557. continue;
  558. if (!PCI_FUNC(dev))
  559. found_multi = header_type & 0x80;
  560. debug("PCI Scan: Found Bus %d, Device %d, Function %d\n",
  561. PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
  562. pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
  563. pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
  564. #ifdef CONFIG_PCI_FIXUP_DEV
  565. board_pci_fixup_dev(hose, dev, vendor, device, class);
  566. #endif
  567. #ifdef CONFIG_PCI_SCAN_SHOW
  568. indent++;
  569. /* Print leading space, including bus indentation */
  570. printf("%*c", indent + 1, ' ');
  571. if (pci_print_dev(hose, dev)) {
  572. printf("%02x:%02x.%-*x - %04x:%04x - %s\n",
  573. PCI_BUS(dev), PCI_DEV(dev), 6 - indent, PCI_FUNC(dev),
  574. vendor, device, pci_class_str(class >> 8));
  575. }
  576. #endif
  577. #ifdef CONFIG_PCI_PNP
  578. sub_bus = max((unsigned int)pciauto_config_device(hose, dev),
  579. sub_bus);
  580. #else
  581. cfg = pci_find_config(hose, class, vendor, device,
  582. PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
  583. if (cfg) {
  584. cfg->config_device(hose, dev, cfg);
  585. sub_bus = max(sub_bus,
  586. (unsigned int)hose->current_busno);
  587. }
  588. #endif
  589. #ifdef CONFIG_PCI_SCAN_SHOW
  590. indent--;
  591. #endif
  592. if (hose->fixup_irq)
  593. hose->fixup_irq(hose, dev);
  594. }
  595. return sub_bus;
  596. }
  597. int pci_hose_scan(struct pci_controller *hose)
  598. {
  599. #if defined(CONFIG_PCI_BOOTDELAY)
  600. char *s;
  601. int i;
  602. if (!gd->pcidelay_done) {
  603. /* wait "pcidelay" ms (if defined)... */
  604. s = getenv("pcidelay");
  605. if (s) {
  606. int val = simple_strtoul(s, NULL, 10);
  607. for (i = 0; i < val; i++)
  608. udelay(1000);
  609. }
  610. gd->pcidelay_done = 1;
  611. }
  612. #endif /* CONFIG_PCI_BOOTDELAY */
  613. /*
  614. * Start scan at current_busno.
  615. * PCIe will start scan at first_busno+1.
  616. */
  617. /* For legacy support, ensure current >= first */
  618. if (hose->first_busno > hose->current_busno)
  619. hose->current_busno = hose->first_busno;
  620. #ifdef CONFIG_PCI_PNP
  621. pciauto_config_init(hose);
  622. #endif
  623. return pci_hose_scan_bus(hose, hose->current_busno);
  624. }
  625. void pci_init(void)
  626. {
  627. hose_head = NULL;
  628. /* now call board specific pci_init()... */
  629. pci_init_board();
  630. }
  631. /* Returns the address of the requested capability structure within the
  632. * device's PCI configuration space or 0 in case the device does not
  633. * support it.
  634. * */
  635. int pci_hose_find_capability(struct pci_controller *hose, pci_dev_t dev,
  636. int cap)
  637. {
  638. int pos;
  639. u8 hdr_type;
  640. pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &hdr_type);
  641. pos = pci_hose_find_cap_start(hose, dev, hdr_type & 0x7F);
  642. if (pos)
  643. pos = pci_find_cap(hose, dev, pos, cap);
  644. return pos;
  645. }
  646. /* Find the header pointer to the Capabilities*/
  647. int pci_hose_find_cap_start(struct pci_controller *hose, pci_dev_t dev,
  648. u8 hdr_type)
  649. {
  650. u16 status;
  651. pci_hose_read_config_word(hose, dev, PCI_STATUS, &status);
  652. if (!(status & PCI_STATUS_CAP_LIST))
  653. return 0;
  654. switch (hdr_type) {
  655. case PCI_HEADER_TYPE_NORMAL:
  656. case PCI_HEADER_TYPE_BRIDGE:
  657. return PCI_CAPABILITY_LIST;
  658. case PCI_HEADER_TYPE_CARDBUS:
  659. return PCI_CB_CAPABILITY_LIST;
  660. default:
  661. return 0;
  662. }
  663. }
  664. int pci_find_cap(struct pci_controller *hose, pci_dev_t dev, int pos, int cap)
  665. {
  666. int ttl = PCI_FIND_CAP_TTL;
  667. u8 id;
  668. u8 next_pos;
  669. while (ttl--) {
  670. pci_hose_read_config_byte(hose, dev, pos, &next_pos);
  671. if (next_pos < CAP_START_POS)
  672. break;
  673. next_pos &= ~3;
  674. pos = (int) next_pos;
  675. pci_hose_read_config_byte(hose, dev,
  676. pos + PCI_CAP_LIST_ID, &id);
  677. if (id == 0xff)
  678. break;
  679. if (id == cap)
  680. return pos;
  681. pos += PCI_CAP_LIST_NEXT;
  682. }
  683. return 0;
  684. }