pci.c 14 KB

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