pci.c 14 KB

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