pci.c 14 KB

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