pci.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*
  27. * PCI routines
  28. */
  29. #include <common.h>
  30. #include <command.h>
  31. #include <asm/processor.h>
  32. #include <asm/io.h>
  33. #include <pci.h>
  34. #define PCI_HOSE_OP(rw, size, type) \
  35. int pci_hose_##rw##_config_##size(struct pci_controller *hose, \
  36. pci_dev_t dev, \
  37. int offset, type value) \
  38. { \
  39. return hose->rw##_##size(hose, dev, offset, value); \
  40. }
  41. PCI_HOSE_OP(read, byte, u8 *)
  42. PCI_HOSE_OP(read, word, u16 *)
  43. PCI_HOSE_OP(read, dword, u32 *)
  44. PCI_HOSE_OP(write, byte, u8)
  45. PCI_HOSE_OP(write, word, u16)
  46. PCI_HOSE_OP(write, dword, u32)
  47. #ifndef CONFIG_IXP425
  48. #define PCI_OP(rw, size, type, error_code) \
  49. int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \
  50. { \
  51. struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \
  52. \
  53. if (!hose) \
  54. { \
  55. error_code; \
  56. return -1; \
  57. } \
  58. \
  59. return pci_hose_##rw##_config_##size(hose, dev, offset, value); \
  60. }
  61. PCI_OP(read, byte, u8 *, *value = 0xff)
  62. PCI_OP(read, word, u16 *, *value = 0xffff)
  63. PCI_OP(read, dword, u32 *, *value = 0xffffffff)
  64. PCI_OP(write, byte, u8, )
  65. PCI_OP(write, word, u16, )
  66. PCI_OP(write, dword, u32, )
  67. #endif /* CONFIG_IXP425 */
  68. #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \
  69. int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
  70. pci_dev_t dev, \
  71. int offset, type val) \
  72. { \
  73. u32 val32; \
  74. \
  75. if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0) { \
  76. *val = -1; \
  77. return -1; \
  78. } \
  79. \
  80. *val = (val32 >> ((offset & (int)off_mask) * 8)); \
  81. \
  82. return 0; \
  83. }
  84. #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \
  85. int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
  86. pci_dev_t dev, \
  87. int offset, type val) \
  88. { \
  89. u32 val32, mask, ldata, shift; \
  90. \
  91. if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
  92. return -1; \
  93. \
  94. shift = ((offset & (int)off_mask) * 8); \
  95. ldata = (((unsigned long)val) & val_mask) << shift; \
  96. mask = val_mask << shift; \
  97. val32 = (val32 & ~mask) | ldata; \
  98. \
  99. if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
  100. return -1; \
  101. \
  102. return 0; \
  103. }
  104. PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
  105. PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
  106. PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
  107. PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
  108. /* Get a virtual address associated with a BAR region */
  109. void *pci_map_bar(pci_dev_t pdev, int bar, int flags)
  110. {
  111. pci_addr_t pci_bus_addr;
  112. u32 bar_response;
  113. /* read BAR address */
  114. pci_read_config_dword(pdev, bar, &bar_response);
  115. pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
  116. /*
  117. * Pass "0" as the length argument to pci_bus_to_virt. The arg
  118. * isn't actualy used on any platform because u-boot assumes a static
  119. * linear mapping. In the future, this could read the BAR size
  120. * and pass that as the size if needed.
  121. */
  122. return pci_bus_to_virt(pdev, pci_bus_addr, flags, 0, MAP_NOCACHE);
  123. }
  124. /*
  125. *
  126. */
  127. static struct pci_controller* hose_head = NULL;
  128. void pci_register_hose(struct pci_controller* hose)
  129. {
  130. struct pci_controller **phose = &hose_head;
  131. while(*phose)
  132. phose = &(*phose)->next;
  133. hose->next = NULL;
  134. *phose = hose;
  135. }
  136. struct pci_controller *pci_bus_to_hose (int bus)
  137. {
  138. struct pci_controller *hose;
  139. for (hose = hose_head; hose; hose = hose->next)
  140. if (bus >= hose->first_busno && bus <= hose->last_busno)
  141. return hose;
  142. printf("pci_bus_to_hose() failed\n");
  143. return NULL;
  144. }
  145. int pci_last_busno(void)
  146. {
  147. struct pci_controller *hose = hose_head;
  148. if (!hose)
  149. return -1;
  150. while (hose->next)
  151. hose = hose->next;
  152. return hose->last_busno;
  153. }
  154. #ifndef CONFIG_IXP425
  155. pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
  156. {
  157. struct pci_controller * hose;
  158. u16 vendor, device;
  159. u8 header_type;
  160. pci_dev_t bdf;
  161. int i, bus, found_multi = 0;
  162. for (hose = hose_head; hose; hose = hose->next)
  163. {
  164. #ifdef CONFIG_SYS_SCSI_SCAN_BUS_REVERSE
  165. for (bus = hose->last_busno; bus >= hose->first_busno; bus--)
  166. #else
  167. for (bus = hose->first_busno; bus <= hose->last_busno; bus++)
  168. #endif
  169. for (bdf = PCI_BDF(bus,0,0);
  170. #if defined(CONFIG_ELPPC) || defined(CONFIG_PPMC7XX)
  171. bdf < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
  172. #else
  173. bdf < PCI_BDF(bus+1,0,0);
  174. #endif
  175. bdf += PCI_BDF(0,0,1))
  176. {
  177. if (!PCI_FUNC(bdf)) {
  178. pci_read_config_byte(bdf,
  179. PCI_HEADER_TYPE,
  180. &header_type);
  181. found_multi = header_type & 0x80;
  182. } else {
  183. if (!found_multi)
  184. continue;
  185. }
  186. pci_read_config_word(bdf,
  187. PCI_VENDOR_ID,
  188. &vendor);
  189. pci_read_config_word(bdf,
  190. PCI_DEVICE_ID,
  191. &device);
  192. for (i=0; ids[i].vendor != 0; i++)
  193. if (vendor == ids[i].vendor &&
  194. device == ids[i].device)
  195. {
  196. if (index <= 0)
  197. return bdf;
  198. index--;
  199. }
  200. }
  201. }
  202. return (-1);
  203. }
  204. #endif /* CONFIG_IXP425 */
  205. pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
  206. {
  207. static struct pci_device_id ids[2] = {{}, {0, 0}};
  208. ids[0].vendor = vendor;
  209. ids[0].device = device;
  210. return pci_find_devices(ids, index);
  211. }
  212. /*
  213. *
  214. */
  215. int __pci_hose_phys_to_bus (struct pci_controller *hose,
  216. phys_addr_t phys_addr,
  217. unsigned long flags,
  218. unsigned long skip_mask,
  219. pci_addr_t *ba)
  220. {
  221. struct pci_region *res;
  222. pci_addr_t bus_addr;
  223. int i;
  224. for (i = 0; i < hose->region_count; i++) {
  225. res = &hose->regions[i];
  226. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  227. continue;
  228. if (res->flags & skip_mask)
  229. continue;
  230. bus_addr = phys_addr - res->phys_start + res->bus_start;
  231. if (bus_addr >= res->bus_start &&
  232. bus_addr < res->bus_start + res->size) {
  233. *ba = bus_addr;
  234. return 0;
  235. }
  236. }
  237. return 1;
  238. }
  239. pci_addr_t pci_hose_phys_to_bus (struct pci_controller *hose,
  240. phys_addr_t phys_addr,
  241. unsigned long flags)
  242. {
  243. pci_addr_t bus_addr = 0;
  244. int ret;
  245. if (!hose) {
  246. puts ("pci_hose_phys_to_bus: invalid hose\n");
  247. return bus_addr;
  248. }
  249. /* if PCI_REGION_MEM is set we do a two pass search with preference
  250. * on matches that don't have PCI_REGION_SYS_MEMORY set */
  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. /* if PCI_REGION_MEM is set we do a two pass search with preference
  295. * on matches that don't have PCI_REGION_SYS_MEMORY set */
  296. if ((flags & PCI_REGION_MEM) == PCI_REGION_MEM) {
  297. ret = __pci_hose_bus_to_phys(hose, bus_addr,
  298. flags, PCI_REGION_SYS_MEMORY, &phys_addr);
  299. if (!ret)
  300. return phys_addr;
  301. }
  302. ret = __pci_hose_bus_to_phys(hose, bus_addr, flags, 0, &phys_addr);
  303. if (ret)
  304. puts ("pci_hose_bus_to_phys: invalid physical address\n");
  305. return phys_addr;
  306. }
  307. /*
  308. *
  309. */
  310. int pci_hose_config_device(struct pci_controller *hose,
  311. pci_dev_t dev,
  312. unsigned long io,
  313. pci_addr_t mem,
  314. unsigned long command)
  315. {
  316. unsigned int bar_response, old_command;
  317. pci_addr_t bar_value;
  318. pci_size_t bar_size;
  319. unsigned char pin;
  320. int bar, found_mem64;
  321. debug ("PCI Config: I/O=0x%lx, Memory=0x%llx, Command=0x%lx\n",
  322. io, (u64)mem, command);
  323. pci_hose_write_config_dword (hose, dev, PCI_COMMAND, 0);
  324. for (bar = PCI_BASE_ADDRESS_0; bar < PCI_BASE_ADDRESS_5; bar += 4) {
  325. pci_hose_write_config_dword (hose, dev, bar, 0xffffffff);
  326. pci_hose_read_config_dword (hose, dev, bar, &bar_response);
  327. if (!bar_response)
  328. continue;
  329. found_mem64 = 0;
  330. /* Check the BAR type and set our address mask */
  331. if (bar_response & PCI_BASE_ADDRESS_SPACE) {
  332. bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
  333. /* round up region base address to a multiple of size */
  334. io = ((io - 1) | (bar_size - 1)) + 1;
  335. bar_value = io;
  336. /* compute new region base address */
  337. io = io + bar_size;
  338. } else {
  339. if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  340. PCI_BASE_ADDRESS_MEM_TYPE_64) {
  341. u32 bar_response_upper;
  342. u64 bar64;
  343. pci_hose_write_config_dword(hose, dev, bar+4, 0xffffffff);
  344. pci_hose_read_config_dword(hose, dev, bar+4, &bar_response_upper);
  345. bar64 = ((u64)bar_response_upper << 32) | bar_response;
  346. bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK) + 1;
  347. found_mem64 = 1;
  348. } else {
  349. bar_size = (u32)(~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1);
  350. }
  351. /* round up region base address to multiple of size */
  352. mem = ((mem - 1) | (bar_size - 1)) + 1;
  353. bar_value = mem;
  354. /* compute new region base address */
  355. mem = mem + bar_size;
  356. }
  357. /* Write it out and update our limit */
  358. pci_hose_write_config_dword (hose, dev, bar, (u32)bar_value);
  359. if (found_mem64) {
  360. bar += 4;
  361. #ifdef CONFIG_SYS_PCI_64BIT
  362. pci_hose_write_config_dword(hose, dev, bar, (u32)(bar_value>>32));
  363. #else
  364. pci_hose_write_config_dword (hose, dev, bar, 0x00000000);
  365. #endif
  366. }
  367. }
  368. /* Configure Cache Line Size Register */
  369. pci_hose_write_config_byte (hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
  370. /* Configure Latency Timer */
  371. pci_hose_write_config_byte (hose, dev, PCI_LATENCY_TIMER, 0x80);
  372. /* Disable interrupt line, if device says it wants to use interrupts */
  373. pci_hose_read_config_byte (hose, dev, PCI_INTERRUPT_PIN, &pin);
  374. if (pin != 0) {
  375. pci_hose_write_config_byte (hose, dev, PCI_INTERRUPT_LINE, 0xff);
  376. }
  377. pci_hose_read_config_dword (hose, dev, PCI_COMMAND, &old_command);
  378. pci_hose_write_config_dword (hose, dev, PCI_COMMAND,
  379. (old_command & 0xffff0000) | command);
  380. return 0;
  381. }
  382. /*
  383. *
  384. */
  385. struct pci_config_table *pci_find_config(struct pci_controller *hose,
  386. unsigned short class,
  387. unsigned int vendor,
  388. unsigned int device,
  389. unsigned int bus,
  390. unsigned int dev,
  391. unsigned int func)
  392. {
  393. struct pci_config_table *table;
  394. for (table = hose->config_table; table && table->vendor; table++) {
  395. if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
  396. (table->device == PCI_ANY_ID || table->device == device) &&
  397. (table->class == PCI_ANY_ID || table->class == class) &&
  398. (table->bus == PCI_ANY_ID || table->bus == bus) &&
  399. (table->dev == PCI_ANY_ID || table->dev == dev) &&
  400. (table->func == PCI_ANY_ID || table->func == func)) {
  401. return table;
  402. }
  403. }
  404. return NULL;
  405. }
  406. void pci_cfgfunc_config_device(struct pci_controller *hose,
  407. pci_dev_t dev,
  408. struct pci_config_table *entry)
  409. {
  410. pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1], entry->priv[2]);
  411. }
  412. void pci_cfgfunc_do_nothing(struct pci_controller *hose,
  413. pci_dev_t dev, struct pci_config_table *entry)
  414. {
  415. }
  416. /*
  417. *
  418. */
  419. /* HJF: Changed this to return int. I think this is required
  420. * to get the correct result when scanning bridges
  421. */
  422. extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
  423. extern void pciauto_config_init(struct pci_controller *hose);
  424. int __pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
  425. {
  426. /*
  427. * Check if pci device should be skipped in configuration
  428. */
  429. if (dev == PCI_BDF(hose->first_busno, 0, 0)) {
  430. #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
  431. /*
  432. * Only skip configuration if "pciconfighost" is not set
  433. */
  434. if (getenv("pciconfighost") == NULL)
  435. return 1;
  436. #else
  437. return 1;
  438. #endif
  439. }
  440. return 0;
  441. }
  442. int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
  443. __attribute__((weak, alias("__pci_skip_dev")));
  444. #ifdef CONFIG_PCI_SCAN_SHOW
  445. int __pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
  446. {
  447. if (dev == PCI_BDF(hose->first_busno, 0, 0))
  448. return 0;
  449. return 1;
  450. }
  451. int pci_print_dev(struct pci_controller *hose, pci_dev_t dev)
  452. __attribute__((weak, alias("__pci_print_dev")));
  453. #endif /* CONFIG_PCI_SCAN_SHOW */
  454. int pci_hose_scan_bus(struct pci_controller *hose, int bus)
  455. {
  456. unsigned int sub_bus, found_multi=0;
  457. unsigned short vendor, device, class;
  458. unsigned char header_type;
  459. struct pci_config_table *cfg;
  460. pci_dev_t dev;
  461. sub_bus = bus;
  462. for (dev = PCI_BDF(bus,0,0);
  463. dev < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
  464. dev += PCI_BDF(0,0,1)) {
  465. if (pci_skip_dev(hose, dev))
  466. continue;
  467. if (PCI_FUNC(dev) && !found_multi)
  468. continue;
  469. pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
  470. pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
  471. if (vendor != 0xffff && vendor != 0x0000) {
  472. if (!PCI_FUNC(dev))
  473. found_multi = header_type & 0x80;
  474. debug ("PCI Scan: Found Bus %d, Device %d, Function %d\n",
  475. PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev) );
  476. pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
  477. pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
  478. cfg = pci_find_config(hose, class, vendor, device,
  479. PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
  480. if (cfg) {
  481. cfg->config_device(hose, dev, cfg);
  482. sub_bus = max(sub_bus, hose->current_busno);
  483. #ifdef CONFIG_PCI_PNP
  484. } else {
  485. int n = pciauto_config_device(hose, dev);
  486. sub_bus = max(sub_bus, n);
  487. #endif
  488. }
  489. if (hose->fixup_irq)
  490. hose->fixup_irq(hose, dev);
  491. #ifdef CONFIG_PCI_SCAN_SHOW
  492. if (pci_print_dev(hose, dev)) {
  493. unsigned char int_line;
  494. pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_LINE,
  495. &int_line);
  496. printf(" %02x %02x %04x %04x %04x %02x\n",
  497. PCI_BUS(dev), PCI_DEV(dev), vendor, device, class,
  498. int_line);
  499. }
  500. #endif
  501. }
  502. }
  503. return sub_bus;
  504. }
  505. int pci_hose_scan(struct pci_controller *hose)
  506. {
  507. /* Start scan at current_busno.
  508. * PCIe will start scan at first_busno+1.
  509. */
  510. /* For legacy support, ensure current>=first */
  511. if (hose->first_busno > hose->current_busno)
  512. hose->current_busno = hose->first_busno;
  513. #ifdef CONFIG_PCI_PNP
  514. pciauto_config_init(hose);
  515. #endif
  516. return pci_hose_scan_bus(hose, hose->current_busno);
  517. }
  518. void pci_init(void)
  519. {
  520. #if defined(CONFIG_PCI_BOOTDELAY)
  521. char *s;
  522. int i;
  523. /* wait "pcidelay" ms (if defined)... */
  524. s = getenv ("pcidelay");
  525. if (s) {
  526. int val = simple_strtoul (s, NULL, 10);
  527. for (i=0; i<val; i++)
  528. udelay (1000);
  529. }
  530. #endif /* CONFIG_PCI_BOOTDELAY */
  531. /* now call board specific pci_init()... */
  532. pci_init_board();
  533. }