pci.c 18 KB

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