pci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. #ifdef CONFIG_PCI
  31. #include <command.h>
  32. #include <asm/processor.h>
  33. #include <asm/io.h>
  34. #include <pci.h>
  35. #define PCI_HOSE_OP(rw, size, type) \
  36. int pci_hose_##rw##_config_##size(struct pci_controller *hose, \
  37. pci_dev_t dev, \
  38. int offset, type value) \
  39. { \
  40. return hose->rw##_##size(hose, dev, offset, value); \
  41. }
  42. PCI_HOSE_OP(read, byte, u8 *)
  43. PCI_HOSE_OP(read, word, u16 *)
  44. PCI_HOSE_OP(read, dword, u32 *)
  45. PCI_HOSE_OP(write, byte, u8)
  46. PCI_HOSE_OP(write, word, u16)
  47. PCI_HOSE_OP(write, dword, u32)
  48. #ifndef CONFIG_IXP425
  49. #define PCI_OP(rw, size, type, error_code) \
  50. int pci_##rw##_config_##size(pci_dev_t dev, int offset, type value) \
  51. { \
  52. struct pci_controller *hose = pci_bus_to_hose(PCI_BUS(dev)); \
  53. \
  54. if (!hose) \
  55. { \
  56. error_code; \
  57. return -1; \
  58. } \
  59. \
  60. return pci_hose_##rw##_config_##size(hose, dev, offset, value); \
  61. }
  62. PCI_OP(read, byte, u8 *, *value = 0xff)
  63. PCI_OP(read, word, u16 *, *value = 0xffff)
  64. PCI_OP(read, dword, u32 *, *value = 0xffffffff)
  65. PCI_OP(write, byte, u8, )
  66. PCI_OP(write, word, u16, )
  67. PCI_OP(write, dword, u32, )
  68. #endif /* CONFIG_IXP425 */
  69. #define PCI_READ_VIA_DWORD_OP(size, type, off_mask) \
  70. int pci_hose_read_config_##size##_via_dword(struct pci_controller *hose,\
  71. pci_dev_t dev, \
  72. int offset, type val) \
  73. { \
  74. u32 val32; \
  75. \
  76. if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
  77. return -1; \
  78. \
  79. *val = (val32 >> ((offset & (int)off_mask) * 8)); \
  80. \
  81. return 0; \
  82. }
  83. #define PCI_WRITE_VIA_DWORD_OP(size, type, off_mask, val_mask) \
  84. int pci_hose_write_config_##size##_via_dword(struct pci_controller *hose,\
  85. pci_dev_t dev, \
  86. int offset, type val) \
  87. { \
  88. u32 val32, mask, ldata, shift; \
  89. \
  90. if (pci_hose_read_config_dword(hose, dev, offset & 0xfc, &val32) < 0)\
  91. return -1; \
  92. \
  93. shift = ((offset & (int)off_mask) * 8); \
  94. ldata = (((unsigned long)val) & val_mask) << shift; \
  95. mask = val_mask << shift; \
  96. val32 = (val32 & ~mask) | ldata; \
  97. \
  98. if (pci_hose_write_config_dword(hose, dev, offset & 0xfc, val32) < 0)\
  99. return -1; \
  100. \
  101. return 0; \
  102. }
  103. PCI_READ_VIA_DWORD_OP(byte, u8 *, 0x03)
  104. PCI_READ_VIA_DWORD_OP(word, u16 *, 0x02)
  105. PCI_WRITE_VIA_DWORD_OP(byte, u8, 0x03, 0x000000ff)
  106. PCI_WRITE_VIA_DWORD_OP(word, u16, 0x02, 0x0000ffff)
  107. /*
  108. *
  109. */
  110. static struct pci_controller* hose_head = NULL;
  111. void pci_register_hose(struct pci_controller* hose)
  112. {
  113. struct pci_controller **phose = &hose_head;
  114. while(*phose)
  115. phose = &(*phose)->next;
  116. hose->next = NULL;
  117. *phose = hose;
  118. }
  119. struct pci_controller *pci_bus_to_hose (int bus)
  120. {
  121. struct pci_controller *hose;
  122. for (hose = hose_head; hose; hose = hose->next)
  123. if (bus >= hose->first_busno && bus <= hose->last_busno)
  124. return hose;
  125. debug ("pci_bus_to_hose() failed\n");
  126. return NULL;
  127. }
  128. #ifndef CONFIG_IXP425
  129. pci_dev_t pci_find_devices(struct pci_device_id *ids, int index)
  130. {
  131. struct pci_controller * hose;
  132. u16 vendor, device;
  133. u8 header_type;
  134. pci_dev_t bdf;
  135. int i, bus, found_multi = 0;
  136. for (hose = hose_head; hose; hose = hose->next)
  137. {
  138. #ifdef CFG_SCSI_SCAN_BUS_REVERSE
  139. for (bus = hose->last_busno; bus >= hose->first_busno; bus--)
  140. #else
  141. for (bus = hose->first_busno; bus <= hose->last_busno; bus++)
  142. #endif
  143. for (bdf = PCI_BDF(bus,0,0);
  144. #ifdef CONFIG_ELPPC
  145. bdf < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
  146. #else
  147. bdf < PCI_BDF(bus+1,0,0);
  148. #endif
  149. bdf += PCI_BDF(0,0,1))
  150. {
  151. if (!PCI_FUNC(bdf)) {
  152. pci_read_config_byte(bdf,
  153. PCI_HEADER_TYPE,
  154. &header_type);
  155. found_multi = header_type & 0x80;
  156. } else {
  157. if (!found_multi)
  158. continue;
  159. }
  160. pci_read_config_word(bdf,
  161. PCI_VENDOR_ID,
  162. &vendor);
  163. pci_read_config_word(bdf,
  164. PCI_DEVICE_ID,
  165. &device);
  166. for (i=0; ids[i].vendor != 0; i++)
  167. if (vendor == ids[i].vendor &&
  168. device == ids[i].device)
  169. {
  170. if (index <= 0)
  171. return bdf;
  172. index--;
  173. }
  174. }
  175. }
  176. return (-1);
  177. }
  178. #endif /* CONFIG_IXP425 */
  179. pci_dev_t pci_find_device(unsigned int vendor, unsigned int device, int index)
  180. {
  181. static struct pci_device_id ids[2] = {{}, {0, 0}};
  182. ids[0].vendor = vendor;
  183. ids[0].device = device;
  184. return pci_find_devices(ids, index);
  185. }
  186. /*
  187. *
  188. */
  189. unsigned long pci_hose_phys_to_bus (struct pci_controller *hose,
  190. unsigned long phys_addr,
  191. unsigned long flags)
  192. {
  193. struct pci_region *res;
  194. unsigned long bus_addr;
  195. int i;
  196. if (!hose) {
  197. printf ("pci_hose_phys_to_bus: %s\n", "invalid hose");
  198. goto Done;
  199. }
  200. for (i = 0; i < hose->region_count; i++) {
  201. res = &hose->regions[i];
  202. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  203. continue;
  204. bus_addr = phys_addr - res->phys_start + res->bus_start;
  205. if (bus_addr >= res->bus_start &&
  206. bus_addr < res->bus_start + res->size) {
  207. return bus_addr;
  208. }
  209. }
  210. printf ("pci_hose_phys_to_bus: %s\n", "invalid physical address");
  211. Done:
  212. return 0;
  213. }
  214. unsigned long pci_hose_bus_to_phys(struct pci_controller* hose,
  215. unsigned long bus_addr,
  216. unsigned long flags)
  217. {
  218. struct pci_region *res;
  219. int i;
  220. if (!hose) {
  221. printf ("pci_hose_bus_to_phys: %s\n", "invalid hose");
  222. goto Done;
  223. }
  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 (bus_addr >= res->bus_start &&
  229. bus_addr < res->bus_start + res->size) {
  230. return bus_addr - res->bus_start + res->phys_start;
  231. }
  232. }
  233. printf ("pci_hose_bus_to_phys: %s\n", "invalid physical address");
  234. Done:
  235. return 0;
  236. }
  237. /*
  238. *
  239. */
  240. int pci_hose_config_device(struct pci_controller *hose,
  241. pci_dev_t dev,
  242. unsigned long io,
  243. unsigned long mem,
  244. unsigned long command)
  245. {
  246. unsigned int bar_response, bar_size, bar_value, old_command;
  247. unsigned char pin;
  248. int bar, found_mem64;
  249. debug ("PCI Config: I/O=0x%lx, Memory=0x%lx, Command=0x%lx\n",
  250. io, mem, command);
  251. pci_hose_write_config_dword (hose, dev, PCI_COMMAND, 0);
  252. for (bar = PCI_BASE_ADDRESS_0; bar < PCI_BASE_ADDRESS_5; bar += 4) {
  253. pci_hose_write_config_dword (hose, dev, bar, 0xffffffff);
  254. pci_hose_read_config_dword (hose, dev, bar, &bar_response);
  255. if (!bar_response)
  256. continue;
  257. found_mem64 = 0;
  258. /* Check the BAR type and set our address mask */
  259. if (bar_response & PCI_BASE_ADDRESS_SPACE) {
  260. bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
  261. /* round up region base address to a multiple of size */
  262. io = ((io - 1) | (bar_size - 1)) + 1;
  263. bar_value = io;
  264. /* compute new region base address */
  265. io = io + bar_size;
  266. } else {
  267. if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  268. PCI_BASE_ADDRESS_MEM_TYPE_64)
  269. found_mem64 = 1;
  270. bar_size = ~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1;
  271. /* round up region base address to multiple of size */
  272. mem = ((mem - 1) | (bar_size - 1)) + 1;
  273. bar_value = mem;
  274. /* compute new region base address */
  275. mem = mem + bar_size;
  276. }
  277. /* Write it out and update our limit */
  278. pci_hose_write_config_dword (hose, dev, bar, bar_value);
  279. if (found_mem64) {
  280. bar += 4;
  281. pci_hose_write_config_dword (hose, dev, bar, 0x00000000);
  282. }
  283. }
  284. /* Configure Cache Line Size Register */
  285. pci_hose_write_config_byte (hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
  286. /* Configure Latency Timer */
  287. pci_hose_write_config_byte (hose, dev, PCI_LATENCY_TIMER, 0x80);
  288. /* Disable interrupt line, if device says it wants to use interrupts */
  289. pci_hose_read_config_byte (hose, dev, PCI_INTERRUPT_PIN, &pin);
  290. if (pin != 0) {
  291. pci_hose_write_config_byte (hose, dev, PCI_INTERRUPT_LINE, 0xff);
  292. }
  293. pci_hose_read_config_dword (hose, dev, PCI_COMMAND, &old_command);
  294. pci_hose_write_config_dword (hose, dev, PCI_COMMAND,
  295. (old_command & 0xffff0000) | command);
  296. return 0;
  297. }
  298. /*
  299. *
  300. */
  301. struct pci_config_table *pci_find_config(struct pci_controller *hose,
  302. unsigned short class,
  303. unsigned int vendor,
  304. unsigned int device,
  305. unsigned int bus,
  306. unsigned int dev,
  307. unsigned int func)
  308. {
  309. struct pci_config_table *table;
  310. for (table = hose->config_table; table && table->vendor; table++) {
  311. if ((table->vendor == PCI_ANY_ID || table->vendor == vendor) &&
  312. (table->device == PCI_ANY_ID || table->device == device) &&
  313. (table->class == PCI_ANY_ID || table->class == class) &&
  314. (table->bus == PCI_ANY_ID || table->bus == bus) &&
  315. (table->dev == PCI_ANY_ID || table->dev == dev) &&
  316. (table->func == PCI_ANY_ID || table->func == func)) {
  317. return table;
  318. }
  319. }
  320. return NULL;
  321. }
  322. void pci_cfgfunc_config_device(struct pci_controller *hose,
  323. pci_dev_t dev,
  324. struct pci_config_table *entry)
  325. {
  326. pci_hose_config_device(hose, dev, entry->priv[0], entry->priv[1], entry->priv[2]);
  327. }
  328. void pci_cfgfunc_do_nothing(struct pci_controller *hose,
  329. pci_dev_t dev, struct pci_config_table *entry)
  330. {
  331. }
  332. /*
  333. *
  334. */
  335. /* HJF: Changed this to return int. I think this is required
  336. * to get the correct result when scanning bridges
  337. */
  338. extern int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev);
  339. extern void pciauto_config_init(struct pci_controller *hose);
  340. int pci_hose_scan_bus(struct pci_controller *hose, int bus)
  341. {
  342. unsigned int sub_bus, found_multi=0;
  343. unsigned short vendor, device, class;
  344. unsigned char header_type;
  345. struct pci_config_table *cfg;
  346. pci_dev_t dev;
  347. sub_bus = bus;
  348. for (dev = PCI_BDF(bus,0,0);
  349. dev < PCI_BDF(bus,PCI_MAX_PCI_DEVICES-1,PCI_MAX_PCI_FUNCTIONS-1);
  350. dev += PCI_BDF(0,0,1))
  351. {
  352. /* Skip our host bridge */
  353. if ( dev == PCI_BDF(hose->first_busno,0,0) ) {
  354. #if defined(CONFIG_PCI_CONFIG_HOST_BRIDGE) /* don't skip host bridge */
  355. /*
  356. * Only skip hostbridge configuration if "pciconfighost" is not set
  357. */
  358. if (getenv("pciconfighost") == NULL) {
  359. continue; /* Skip our host bridge */
  360. }
  361. #else
  362. continue; /* Skip our host bridge */
  363. #endif
  364. }
  365. if (PCI_FUNC(dev) && !found_multi)
  366. continue;
  367. pci_hose_read_config_byte(hose, dev, PCI_HEADER_TYPE, &header_type);
  368. pci_hose_read_config_word(hose, dev, PCI_VENDOR_ID, &vendor);
  369. if (vendor != 0xffff && vendor != 0x0000) {
  370. if (!PCI_FUNC(dev))
  371. found_multi = header_type & 0x80;
  372. debug ("PCI Scan: Found Bus %d, Device %d, Function %d\n",
  373. PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev) );
  374. pci_hose_read_config_word(hose, dev, PCI_DEVICE_ID, &device);
  375. pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
  376. cfg = pci_find_config(hose, class, vendor, device,
  377. PCI_BUS(dev), PCI_DEV(dev), PCI_FUNC(dev));
  378. if (cfg) {
  379. cfg->config_device(hose, dev, cfg);
  380. #ifdef CONFIG_PCI_PNP
  381. } else {
  382. int n = pciauto_config_device(hose, dev);
  383. sub_bus = max(sub_bus, n);
  384. #endif
  385. }
  386. if (hose->fixup_irq)
  387. hose->fixup_irq(hose, dev);
  388. #ifdef CONFIG_PCI_SCAN_SHOW
  389. /* Skip our host bridge */
  390. if ( dev != PCI_BDF(hose->first_busno,0,0) ) {
  391. unsigned char int_line;
  392. pci_hose_read_config_byte(hose, dev, PCI_INTERRUPT_LINE,
  393. &int_line);
  394. printf(" %02x %02x %04x %04x %04x %02x\n",
  395. PCI_BUS(dev), PCI_DEV(dev), vendor, device, class,
  396. int_line);
  397. }
  398. #endif
  399. }
  400. }
  401. return sub_bus;
  402. }
  403. int pci_hose_scan(struct pci_controller *hose)
  404. {
  405. #ifdef CONFIG_PCI_PNP
  406. pciauto_config_init(hose);
  407. #endif
  408. return pci_hose_scan_bus(hose, hose->first_busno);
  409. }
  410. void pci_init(void)
  411. {
  412. #if defined(CONFIG_PCI_BOOTDELAY)
  413. char *s;
  414. int i;
  415. /* wait "pcidelay" ms (if defined)... */
  416. s = getenv ("pcidelay");
  417. if (s) {
  418. int val = simple_strtoul (s, NULL, 10);
  419. for (i=0; i<val; i++)
  420. udelay (1000);
  421. }
  422. #endif /* CONFIG_PCI_BOOTDELAY */
  423. /* now call board specific pci_init()... */
  424. pci_init_board();
  425. }
  426. #endif /* CONFIG_PCI */