pci-uclass.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2014 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <pci.h>
  10. #include <asm/io.h>
  11. #include <dm/device-internal.h>
  12. #include <dm/lists.h>
  13. #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
  14. #include <asm/fsp/fsp_support.h>
  15. #endif
  16. #include "pci_internal.h"
  17. DECLARE_GLOBAL_DATA_PTR;
  18. int pci_get_bus(int busnum, struct udevice **busp)
  19. {
  20. int ret;
  21. ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
  22. /* Since buses may not be numbered yet try a little harder with bus 0 */
  23. if (ret == -ENODEV) {
  24. ret = uclass_first_device_err(UCLASS_PCI, busp);
  25. if (ret)
  26. return ret;
  27. ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
  28. }
  29. return ret;
  30. }
  31. struct udevice *pci_get_controller(struct udevice *dev)
  32. {
  33. while (device_is_on_pci_bus(dev))
  34. dev = dev->parent;
  35. return dev;
  36. }
  37. pci_dev_t dm_pci_get_bdf(struct udevice *dev)
  38. {
  39. struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
  40. struct udevice *bus = dev->parent;
  41. return PCI_ADD_BUS(bus->seq, pplat->devfn);
  42. }
  43. /**
  44. * pci_get_bus_max() - returns the bus number of the last active bus
  45. *
  46. * @return last bus number, or -1 if no active buses
  47. */
  48. static int pci_get_bus_max(void)
  49. {
  50. struct udevice *bus;
  51. struct uclass *uc;
  52. int ret = -1;
  53. ret = uclass_get(UCLASS_PCI, &uc);
  54. uclass_foreach_dev(bus, uc) {
  55. if (bus->seq > ret)
  56. ret = bus->seq;
  57. }
  58. debug("%s: ret=%d\n", __func__, ret);
  59. return ret;
  60. }
  61. int pci_last_busno(void)
  62. {
  63. return pci_get_bus_max();
  64. }
  65. int pci_get_ff(enum pci_size_t size)
  66. {
  67. switch (size) {
  68. case PCI_SIZE_8:
  69. return 0xff;
  70. case PCI_SIZE_16:
  71. return 0xffff;
  72. default:
  73. return 0xffffffff;
  74. }
  75. }
  76. static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
  77. ofnode *rnode)
  78. {
  79. struct fdt_pci_addr addr;
  80. ofnode node;
  81. int ret;
  82. dev_for_each_subnode(node, bus) {
  83. ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
  84. &addr);
  85. if (ret)
  86. continue;
  87. if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
  88. continue;
  89. *rnode = node;
  90. break;
  91. }
  92. };
  93. int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
  94. struct udevice **devp)
  95. {
  96. struct udevice *dev;
  97. for (device_find_first_child(bus, &dev);
  98. dev;
  99. device_find_next_child(&dev)) {
  100. struct pci_child_platdata *pplat;
  101. pplat = dev_get_parent_platdata(dev);
  102. if (pplat && pplat->devfn == find_devfn) {
  103. *devp = dev;
  104. return 0;
  105. }
  106. }
  107. return -ENODEV;
  108. }
  109. int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
  110. {
  111. struct udevice *bus;
  112. int ret;
  113. ret = pci_get_bus(PCI_BUS(bdf), &bus);
  114. if (ret)
  115. return ret;
  116. return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
  117. }
  118. static int pci_device_matches_ids(struct udevice *dev,
  119. struct pci_device_id *ids)
  120. {
  121. struct pci_child_platdata *pplat;
  122. int i;
  123. pplat = dev_get_parent_platdata(dev);
  124. if (!pplat)
  125. return -EINVAL;
  126. for (i = 0; ids[i].vendor != 0; i++) {
  127. if (pplat->vendor == ids[i].vendor &&
  128. pplat->device == ids[i].device)
  129. return i;
  130. }
  131. return -EINVAL;
  132. }
  133. int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
  134. int *indexp, struct udevice **devp)
  135. {
  136. struct udevice *dev;
  137. /* Scan all devices on this bus */
  138. for (device_find_first_child(bus, &dev);
  139. dev;
  140. device_find_next_child(&dev)) {
  141. if (pci_device_matches_ids(dev, ids) >= 0) {
  142. if ((*indexp)-- <= 0) {
  143. *devp = dev;
  144. return 0;
  145. }
  146. }
  147. }
  148. return -ENODEV;
  149. }
  150. int pci_find_device_id(struct pci_device_id *ids, int index,
  151. struct udevice **devp)
  152. {
  153. struct udevice *bus;
  154. /* Scan all known buses */
  155. for (uclass_first_device(UCLASS_PCI, &bus);
  156. bus;
  157. uclass_next_device(&bus)) {
  158. if (!pci_bus_find_devices(bus, ids, &index, devp))
  159. return 0;
  160. }
  161. *devp = NULL;
  162. return -ENODEV;
  163. }
  164. static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
  165. unsigned int device, int *indexp,
  166. struct udevice **devp)
  167. {
  168. struct pci_child_platdata *pplat;
  169. struct udevice *dev;
  170. for (device_find_first_child(bus, &dev);
  171. dev;
  172. device_find_next_child(&dev)) {
  173. pplat = dev_get_parent_platdata(dev);
  174. if (pplat->vendor == vendor && pplat->device == device) {
  175. if (!(*indexp)--) {
  176. *devp = dev;
  177. return 0;
  178. }
  179. }
  180. }
  181. return -ENODEV;
  182. }
  183. int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
  184. struct udevice **devp)
  185. {
  186. struct udevice *bus;
  187. /* Scan all known buses */
  188. for (uclass_first_device(UCLASS_PCI, &bus);
  189. bus;
  190. uclass_next_device(&bus)) {
  191. if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
  192. return device_probe(*devp);
  193. }
  194. *devp = NULL;
  195. return -ENODEV;
  196. }
  197. int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
  198. {
  199. struct udevice *dev;
  200. /* Scan all known buses */
  201. for (pci_find_first_device(&dev);
  202. dev;
  203. pci_find_next_device(&dev)) {
  204. struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
  205. if (pplat->class == find_class && !index--) {
  206. *devp = dev;
  207. return device_probe(*devp);
  208. }
  209. }
  210. *devp = NULL;
  211. return -ENODEV;
  212. }
  213. int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
  214. unsigned long value, enum pci_size_t size)
  215. {
  216. struct dm_pci_ops *ops;
  217. ops = pci_get_ops(bus);
  218. if (!ops->write_config)
  219. return -ENOSYS;
  220. return ops->write_config(bus, bdf, offset, value, size);
  221. }
  222. int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
  223. u32 clr, u32 set)
  224. {
  225. ulong val;
  226. int ret;
  227. ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
  228. if (ret)
  229. return ret;
  230. val &= ~clr;
  231. val |= set;
  232. return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
  233. }
  234. int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
  235. enum pci_size_t size)
  236. {
  237. struct udevice *bus;
  238. int ret;
  239. ret = pci_get_bus(PCI_BUS(bdf), &bus);
  240. if (ret)
  241. return ret;
  242. return pci_bus_write_config(bus, bdf, offset, value, size);
  243. }
  244. int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
  245. enum pci_size_t size)
  246. {
  247. struct udevice *bus;
  248. for (bus = dev; device_is_on_pci_bus(bus);)
  249. bus = bus->parent;
  250. return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
  251. size);
  252. }
  253. int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
  254. {
  255. return pci_write_config(bdf, offset, value, PCI_SIZE_32);
  256. }
  257. int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
  258. {
  259. return pci_write_config(bdf, offset, value, PCI_SIZE_16);
  260. }
  261. int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
  262. {
  263. return pci_write_config(bdf, offset, value, PCI_SIZE_8);
  264. }
  265. int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
  266. {
  267. return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
  268. }
  269. int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
  270. {
  271. return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
  272. }
  273. int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
  274. {
  275. return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
  276. }
  277. int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
  278. unsigned long *valuep, enum pci_size_t size)
  279. {
  280. struct dm_pci_ops *ops;
  281. ops = pci_get_ops(bus);
  282. if (!ops->read_config)
  283. return -ENOSYS;
  284. return ops->read_config(bus, bdf, offset, valuep, size);
  285. }
  286. int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
  287. enum pci_size_t size)
  288. {
  289. struct udevice *bus;
  290. int ret;
  291. ret = pci_get_bus(PCI_BUS(bdf), &bus);
  292. if (ret)
  293. return ret;
  294. return pci_bus_read_config(bus, bdf, offset, valuep, size);
  295. }
  296. int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
  297. enum pci_size_t size)
  298. {
  299. struct udevice *bus;
  300. for (bus = dev; device_is_on_pci_bus(bus);)
  301. bus = bus->parent;
  302. return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
  303. size);
  304. }
  305. int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
  306. {
  307. unsigned long value;
  308. int ret;
  309. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
  310. if (ret)
  311. return ret;
  312. *valuep = value;
  313. return 0;
  314. }
  315. int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
  316. {
  317. unsigned long value;
  318. int ret;
  319. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
  320. if (ret)
  321. return ret;
  322. *valuep = value;
  323. return 0;
  324. }
  325. int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
  326. {
  327. unsigned long value;
  328. int ret;
  329. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
  330. if (ret)
  331. return ret;
  332. *valuep = value;
  333. return 0;
  334. }
  335. int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
  336. {
  337. unsigned long value;
  338. int ret;
  339. ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
  340. if (ret)
  341. return ret;
  342. *valuep = value;
  343. return 0;
  344. }
  345. int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
  346. {
  347. unsigned long value;
  348. int ret;
  349. ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
  350. if (ret)
  351. return ret;
  352. *valuep = value;
  353. return 0;
  354. }
  355. int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
  356. {
  357. unsigned long value;
  358. int ret;
  359. ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
  360. if (ret)
  361. return ret;
  362. *valuep = value;
  363. return 0;
  364. }
  365. int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
  366. {
  367. u8 val;
  368. int ret;
  369. ret = dm_pci_read_config8(dev, offset, &val);
  370. if (ret)
  371. return ret;
  372. val &= ~clr;
  373. val |= set;
  374. return dm_pci_write_config8(dev, offset, val);
  375. }
  376. int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
  377. {
  378. u16 val;
  379. int ret;
  380. ret = dm_pci_read_config16(dev, offset, &val);
  381. if (ret)
  382. return ret;
  383. val &= ~clr;
  384. val |= set;
  385. return dm_pci_write_config16(dev, offset, val);
  386. }
  387. int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
  388. {
  389. u32 val;
  390. int ret;
  391. ret = dm_pci_read_config32(dev, offset, &val);
  392. if (ret)
  393. return ret;
  394. val &= ~clr;
  395. val |= set;
  396. return dm_pci_write_config32(dev, offset, val);
  397. }
  398. static void set_vga_bridge_bits(struct udevice *dev)
  399. {
  400. struct udevice *parent = dev->parent;
  401. u16 bc;
  402. while (parent->seq != 0) {
  403. dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
  404. bc |= PCI_BRIDGE_CTL_VGA;
  405. dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
  406. parent = parent->parent;
  407. }
  408. }
  409. int pci_auto_config_devices(struct udevice *bus)
  410. {
  411. struct pci_controller *hose = bus->uclass_priv;
  412. struct pci_child_platdata *pplat;
  413. unsigned int sub_bus;
  414. struct udevice *dev;
  415. int ret;
  416. sub_bus = bus->seq;
  417. debug("%s: start\n", __func__);
  418. pciauto_config_init(hose);
  419. for (ret = device_find_first_child(bus, &dev);
  420. !ret && dev;
  421. ret = device_find_next_child(&dev)) {
  422. unsigned int max_bus;
  423. int ret;
  424. debug("%s: device %s\n", __func__, dev->name);
  425. ret = dm_pciauto_config_device(dev);
  426. if (ret < 0)
  427. return ret;
  428. max_bus = ret;
  429. sub_bus = max(sub_bus, max_bus);
  430. pplat = dev_get_parent_platdata(dev);
  431. if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
  432. set_vga_bridge_bits(dev);
  433. }
  434. debug("%s: done\n", __func__);
  435. return sub_bus;
  436. }
  437. int pci_generic_mmap_write_config(
  438. struct udevice *bus,
  439. int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
  440. pci_dev_t bdf,
  441. uint offset,
  442. ulong value,
  443. enum pci_size_t size)
  444. {
  445. void *address;
  446. if (addr_f(bus, bdf, offset, &address) < 0)
  447. return 0;
  448. switch (size) {
  449. case PCI_SIZE_8:
  450. writeb(value, address);
  451. return 0;
  452. case PCI_SIZE_16:
  453. writew(value, address);
  454. return 0;
  455. case PCI_SIZE_32:
  456. writel(value, address);
  457. return 0;
  458. default:
  459. return -EINVAL;
  460. }
  461. }
  462. int pci_generic_mmap_read_config(
  463. struct udevice *bus,
  464. int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
  465. pci_dev_t bdf,
  466. uint offset,
  467. ulong *valuep,
  468. enum pci_size_t size)
  469. {
  470. void *address;
  471. if (addr_f(bus, bdf, offset, &address) < 0) {
  472. *valuep = pci_get_ff(size);
  473. return 0;
  474. }
  475. switch (size) {
  476. case PCI_SIZE_8:
  477. *valuep = readb(address);
  478. return 0;
  479. case PCI_SIZE_16:
  480. *valuep = readw(address);
  481. return 0;
  482. case PCI_SIZE_32:
  483. *valuep = readl(address);
  484. return 0;
  485. default:
  486. return -EINVAL;
  487. }
  488. }
  489. int dm_pci_hose_probe_bus(struct udevice *bus)
  490. {
  491. int sub_bus;
  492. int ret;
  493. debug("%s\n", __func__);
  494. sub_bus = pci_get_bus_max() + 1;
  495. debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
  496. dm_pciauto_prescan_setup_bridge(bus, sub_bus);
  497. ret = device_probe(bus);
  498. if (ret) {
  499. debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
  500. ret);
  501. return ret;
  502. }
  503. if (sub_bus != bus->seq) {
  504. printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
  505. __func__, bus->name, bus->seq, sub_bus);
  506. return -EPIPE;
  507. }
  508. sub_bus = pci_get_bus_max();
  509. dm_pciauto_postscan_setup_bridge(bus, sub_bus);
  510. return sub_bus;
  511. }
  512. /**
  513. * pci_match_one_device - Tell if a PCI device structure has a matching
  514. * PCI device id structure
  515. * @id: single PCI device id structure to match
  516. * @find: the PCI device id structure to match against
  517. *
  518. * Returns true if the finding pci_device_id structure matched or false if
  519. * there is no match.
  520. */
  521. static bool pci_match_one_id(const struct pci_device_id *id,
  522. const struct pci_device_id *find)
  523. {
  524. if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
  525. (id->device == PCI_ANY_ID || id->device == find->device) &&
  526. (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
  527. (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
  528. !((id->class ^ find->class) & id->class_mask))
  529. return true;
  530. return false;
  531. }
  532. /**
  533. * pci_find_and_bind_driver() - Find and bind the right PCI driver
  534. *
  535. * This only looks at certain fields in the descriptor.
  536. *
  537. * @parent: Parent bus
  538. * @find_id: Specification of the driver to find
  539. * @bdf: Bus/device/function addreess - see PCI_BDF()
  540. * @devp: Returns a pointer to the device created
  541. * @return 0 if OK, -EPERM if the device is not needed before relocation and
  542. * therefore was not created, other -ve value on error
  543. */
  544. static int pci_find_and_bind_driver(struct udevice *parent,
  545. struct pci_device_id *find_id,
  546. pci_dev_t bdf, struct udevice **devp)
  547. {
  548. struct pci_driver_entry *start, *entry;
  549. ofnode node = ofnode_null();
  550. const char *drv;
  551. int n_ents;
  552. int ret;
  553. char name[30], *str;
  554. bool bridge;
  555. *devp = NULL;
  556. debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
  557. find_id->vendor, find_id->device);
  558. /* Determine optional OF node */
  559. pci_dev_find_ofnode(parent, bdf, &node);
  560. start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
  561. n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
  562. for (entry = start; entry != start + n_ents; entry++) {
  563. const struct pci_device_id *id;
  564. struct udevice *dev;
  565. const struct driver *drv;
  566. for (id = entry->match;
  567. id->vendor || id->subvendor || id->class_mask;
  568. id++) {
  569. if (!pci_match_one_id(id, find_id))
  570. continue;
  571. drv = entry->driver;
  572. /*
  573. * In the pre-relocation phase, we only bind devices
  574. * whose driver has the DM_FLAG_PRE_RELOC set, to save
  575. * precious memory space as on some platforms as that
  576. * space is pretty limited (ie: using Cache As RAM).
  577. */
  578. if (!(gd->flags & GD_FLG_RELOC) &&
  579. !(drv->flags & DM_FLAG_PRE_RELOC))
  580. return -EPERM;
  581. /*
  582. * We could pass the descriptor to the driver as
  583. * platdata (instead of NULL) and allow its bind()
  584. * method to return -ENOENT if it doesn't support this
  585. * device. That way we could continue the search to
  586. * find another driver. For now this doesn't seem
  587. * necesssary, so just bind the first match.
  588. */
  589. ret = device_bind_ofnode(parent, drv, drv->name, NULL,
  590. node, &dev);
  591. if (ret)
  592. goto error;
  593. debug("%s: Match found: %s\n", __func__, drv->name);
  594. dev->driver_data = id->driver_data;
  595. *devp = dev;
  596. return 0;
  597. }
  598. }
  599. bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
  600. /*
  601. * In the pre-relocation phase, we only bind bridge devices to save
  602. * precious memory space as on some platforms as that space is pretty
  603. * limited (ie: using Cache As RAM).
  604. */
  605. if (!(gd->flags & GD_FLG_RELOC) && !bridge)
  606. return -EPERM;
  607. /* Bind a generic driver so that the device can be used */
  608. sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
  609. PCI_FUNC(bdf));
  610. str = strdup(name);
  611. if (!str)
  612. return -ENOMEM;
  613. drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
  614. ret = device_bind_driver_to_node(parent, drv, str, node, devp);
  615. if (ret) {
  616. debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
  617. free(str);
  618. return ret;
  619. }
  620. debug("%s: No match found: bound generic driver instead\n", __func__);
  621. return 0;
  622. error:
  623. debug("%s: No match found: error %d\n", __func__, ret);
  624. return ret;
  625. }
  626. int pci_bind_bus_devices(struct udevice *bus)
  627. {
  628. ulong vendor, device;
  629. ulong header_type;
  630. pci_dev_t bdf, end;
  631. bool found_multi;
  632. int ret;
  633. found_multi = false;
  634. end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
  635. PCI_MAX_PCI_FUNCTIONS - 1);
  636. for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
  637. bdf += PCI_BDF(0, 0, 1)) {
  638. struct pci_child_platdata *pplat;
  639. struct udevice *dev;
  640. ulong class;
  641. if (!PCI_FUNC(bdf))
  642. found_multi = false;
  643. if (PCI_FUNC(bdf) && !found_multi)
  644. continue;
  645. /* Check only the first access, we don't expect problems */
  646. ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
  647. &header_type, PCI_SIZE_8);
  648. if (ret)
  649. goto error;
  650. pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
  651. PCI_SIZE_16);
  652. if (vendor == 0xffff || vendor == 0x0000)
  653. continue;
  654. if (!PCI_FUNC(bdf))
  655. found_multi = header_type & 0x80;
  656. debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
  657. bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
  658. pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
  659. PCI_SIZE_16);
  660. pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
  661. PCI_SIZE_32);
  662. class >>= 8;
  663. /* Find this device in the device tree */
  664. ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
  665. /* If nothing in the device tree, bind a device */
  666. if (ret == -ENODEV) {
  667. struct pci_device_id find_id;
  668. ulong val;
  669. memset(&find_id, '\0', sizeof(find_id));
  670. find_id.vendor = vendor;
  671. find_id.device = device;
  672. find_id.class = class;
  673. if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
  674. pci_bus_read_config(bus, bdf,
  675. PCI_SUBSYSTEM_VENDOR_ID,
  676. &val, PCI_SIZE_32);
  677. find_id.subvendor = val & 0xffff;
  678. find_id.subdevice = val >> 16;
  679. }
  680. ret = pci_find_and_bind_driver(bus, &find_id, bdf,
  681. &dev);
  682. }
  683. if (ret == -EPERM)
  684. continue;
  685. else if (ret)
  686. return ret;
  687. /* Update the platform data */
  688. pplat = dev_get_parent_platdata(dev);
  689. pplat->devfn = PCI_MASK_BUS(bdf);
  690. pplat->vendor = vendor;
  691. pplat->device = device;
  692. pplat->class = class;
  693. }
  694. return 0;
  695. error:
  696. printf("Cannot read bus configuration: %d\n", ret);
  697. return ret;
  698. }
  699. static void decode_regions(struct pci_controller *hose, ofnode parent_node,
  700. ofnode node)
  701. {
  702. int pci_addr_cells, addr_cells, size_cells;
  703. int cells_per_record;
  704. const u32 *prop;
  705. int len;
  706. int i;
  707. prop = ofnode_get_property(node, "ranges", &len);
  708. if (!prop) {
  709. debug("%s: Cannot decode regions\n", __func__);
  710. return;
  711. }
  712. pci_addr_cells = ofnode_read_simple_addr_cells(node);
  713. addr_cells = ofnode_read_simple_addr_cells(parent_node);
  714. size_cells = ofnode_read_simple_size_cells(node);
  715. /* PCI addresses are always 3-cells */
  716. len /= sizeof(u32);
  717. cells_per_record = pci_addr_cells + addr_cells + size_cells;
  718. hose->region_count = 0;
  719. debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
  720. cells_per_record);
  721. for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
  722. u64 pci_addr, addr, size;
  723. int space_code;
  724. u32 flags;
  725. int type;
  726. int pos;
  727. if (len < cells_per_record)
  728. break;
  729. flags = fdt32_to_cpu(prop[0]);
  730. space_code = (flags >> 24) & 3;
  731. pci_addr = fdtdec_get_number(prop + 1, 2);
  732. prop += pci_addr_cells;
  733. addr = fdtdec_get_number(prop, addr_cells);
  734. prop += addr_cells;
  735. size = fdtdec_get_number(prop, size_cells);
  736. prop += size_cells;
  737. debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
  738. __func__, hose->region_count, pci_addr, addr, size, space_code);
  739. if (space_code & 2) {
  740. type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
  741. PCI_REGION_MEM;
  742. } else if (space_code & 1) {
  743. type = PCI_REGION_IO;
  744. } else {
  745. continue;
  746. }
  747. if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
  748. type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
  749. debug(" - beyond the 32-bit boundary, ignoring\n");
  750. continue;
  751. }
  752. pos = -1;
  753. for (i = 0; i < hose->region_count; i++) {
  754. if (hose->regions[i].flags == type)
  755. pos = i;
  756. }
  757. if (pos == -1)
  758. pos = hose->region_count++;
  759. debug(" - type=%d, pos=%d\n", type, pos);
  760. pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
  761. }
  762. /* Add a region for our local memory */
  763. #ifdef CONFIG_NR_DRAM_BANKS
  764. bd_t *bd = gd->bd;
  765. if (!bd)
  766. return;
  767. for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
  768. if (bd->bi_dram[i].size) {
  769. pci_set_region(hose->regions + hose->region_count++,
  770. bd->bi_dram[i].start,
  771. bd->bi_dram[i].start,
  772. bd->bi_dram[i].size,
  773. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  774. }
  775. }
  776. #else
  777. phys_addr_t base = 0, size;
  778. size = gd->ram_size;
  779. #ifdef CONFIG_SYS_SDRAM_BASE
  780. base = CONFIG_SYS_SDRAM_BASE;
  781. #endif
  782. if (gd->pci_ram_top && gd->pci_ram_top < base + size)
  783. size = gd->pci_ram_top - base;
  784. if (size)
  785. pci_set_region(hose->regions + hose->region_count++, base,
  786. base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  787. #endif
  788. return;
  789. }
  790. static int pci_uclass_pre_probe(struct udevice *bus)
  791. {
  792. struct pci_controller *hose;
  793. debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
  794. bus->parent->name);
  795. hose = bus->uclass_priv;
  796. /* For bridges, use the top-level PCI controller */
  797. if (!device_is_on_pci_bus(bus)) {
  798. hose->ctlr = bus;
  799. decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
  800. } else {
  801. struct pci_controller *parent_hose;
  802. parent_hose = dev_get_uclass_priv(bus->parent);
  803. hose->ctlr = parent_hose->bus;
  804. }
  805. hose->bus = bus;
  806. hose->first_busno = bus->seq;
  807. hose->last_busno = bus->seq;
  808. return 0;
  809. }
  810. static int pci_uclass_post_probe(struct udevice *bus)
  811. {
  812. int ret;
  813. debug("%s: probing bus %d\n", __func__, bus->seq);
  814. ret = pci_bind_bus_devices(bus);
  815. if (ret)
  816. return ret;
  817. #ifdef CONFIG_PCI_PNP
  818. ret = pci_auto_config_devices(bus);
  819. if (ret < 0)
  820. return ret;
  821. #endif
  822. #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
  823. /*
  824. * Per Intel FSP specification, we should call FSP notify API to
  825. * inform FSP that PCI enumeration has been done so that FSP will
  826. * do any necessary initialization as required by the chipset's
  827. * BIOS Writer's Guide (BWG).
  828. *
  829. * Unfortunately we have to put this call here as with driver model,
  830. * the enumeration is all done on a lazy basis as needed, so until
  831. * something is touched on PCI it won't happen.
  832. *
  833. * Note we only call this 1) after U-Boot is relocated, and 2)
  834. * root bus has finished probing.
  835. */
  836. if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
  837. ret = fsp_init_phase_pci();
  838. if (ret)
  839. return ret;
  840. }
  841. #endif
  842. return 0;
  843. }
  844. static int pci_uclass_child_post_bind(struct udevice *dev)
  845. {
  846. struct pci_child_platdata *pplat;
  847. struct fdt_pci_addr addr;
  848. int ret;
  849. if (!dev_of_valid(dev))
  850. return 0;
  851. pplat = dev_get_parent_platdata(dev);
  852. /* Extract vendor id and device id if available */
  853. ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
  854. /* Extract the devfn from fdt_pci_addr */
  855. ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_CONFIG, "reg",
  856. &addr);
  857. if (ret) {
  858. if (ret != -ENOENT)
  859. return -EINVAL;
  860. } else {
  861. pplat->devfn = addr.phys_hi & 0xff00;
  862. }
  863. return 0;
  864. }
  865. static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
  866. uint offset, ulong *valuep,
  867. enum pci_size_t size)
  868. {
  869. struct pci_controller *hose = bus->uclass_priv;
  870. return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
  871. }
  872. static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
  873. uint offset, ulong value,
  874. enum pci_size_t size)
  875. {
  876. struct pci_controller *hose = bus->uclass_priv;
  877. return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
  878. }
  879. static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
  880. {
  881. struct udevice *dev;
  882. int ret = 0;
  883. /*
  884. * Scan through all the PCI controllers. On x86 there will only be one
  885. * but that is not necessarily true on other hardware.
  886. */
  887. do {
  888. device_find_first_child(bus, &dev);
  889. if (dev) {
  890. *devp = dev;
  891. return 0;
  892. }
  893. ret = uclass_next_device(&bus);
  894. if (ret)
  895. return ret;
  896. } while (bus);
  897. return 0;
  898. }
  899. int pci_find_next_device(struct udevice **devp)
  900. {
  901. struct udevice *child = *devp;
  902. struct udevice *bus = child->parent;
  903. int ret;
  904. /* First try all the siblings */
  905. *devp = NULL;
  906. while (child) {
  907. device_find_next_child(&child);
  908. if (child) {
  909. *devp = child;
  910. return 0;
  911. }
  912. }
  913. /* We ran out of siblings. Try the next bus */
  914. ret = uclass_next_device(&bus);
  915. if (ret)
  916. return ret;
  917. return bus ? skip_to_next_device(bus, devp) : 0;
  918. }
  919. int pci_find_first_device(struct udevice **devp)
  920. {
  921. struct udevice *bus;
  922. int ret;
  923. *devp = NULL;
  924. ret = uclass_first_device(UCLASS_PCI, &bus);
  925. if (ret)
  926. return ret;
  927. return skip_to_next_device(bus, devp);
  928. }
  929. ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
  930. {
  931. switch (size) {
  932. case PCI_SIZE_8:
  933. return (value >> ((offset & 3) * 8)) & 0xff;
  934. case PCI_SIZE_16:
  935. return (value >> ((offset & 2) * 8)) & 0xffff;
  936. default:
  937. return value;
  938. }
  939. }
  940. ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
  941. enum pci_size_t size)
  942. {
  943. uint off_mask;
  944. uint val_mask, shift;
  945. ulong ldata, mask;
  946. switch (size) {
  947. case PCI_SIZE_8:
  948. off_mask = 3;
  949. val_mask = 0xff;
  950. break;
  951. case PCI_SIZE_16:
  952. off_mask = 2;
  953. val_mask = 0xffff;
  954. break;
  955. default:
  956. return value;
  957. }
  958. shift = (offset & off_mask) * 8;
  959. ldata = (value & val_mask) << shift;
  960. mask = val_mask << shift;
  961. value = (old & ~mask) | ldata;
  962. return value;
  963. }
  964. int pci_get_regions(struct udevice *dev, struct pci_region **iop,
  965. struct pci_region **memp, struct pci_region **prefp)
  966. {
  967. struct udevice *bus = pci_get_controller(dev);
  968. struct pci_controller *hose = dev_get_uclass_priv(bus);
  969. int i;
  970. *iop = NULL;
  971. *memp = NULL;
  972. *prefp = NULL;
  973. for (i = 0; i < hose->region_count; i++) {
  974. switch (hose->regions[i].flags) {
  975. case PCI_REGION_IO:
  976. if (!*iop || (*iop)->size < hose->regions[i].size)
  977. *iop = hose->regions + i;
  978. break;
  979. case PCI_REGION_MEM:
  980. if (!*memp || (*memp)->size < hose->regions[i].size)
  981. *memp = hose->regions + i;
  982. break;
  983. case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
  984. if (!*prefp || (*prefp)->size < hose->regions[i].size)
  985. *prefp = hose->regions + i;
  986. break;
  987. }
  988. }
  989. return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
  990. }
  991. u32 dm_pci_read_bar32(struct udevice *dev, int barnum)
  992. {
  993. u32 addr;
  994. int bar;
  995. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  996. dm_pci_read_config32(dev, bar, &addr);
  997. if (addr & PCI_BASE_ADDRESS_SPACE_IO)
  998. return addr & PCI_BASE_ADDRESS_IO_MASK;
  999. else
  1000. return addr & PCI_BASE_ADDRESS_MEM_MASK;
  1001. }
  1002. void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
  1003. {
  1004. int bar;
  1005. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  1006. dm_pci_write_config32(dev, bar, addr);
  1007. }
  1008. static int _dm_pci_bus_to_phys(struct udevice *ctlr,
  1009. pci_addr_t bus_addr, unsigned long flags,
  1010. unsigned long skip_mask, phys_addr_t *pa)
  1011. {
  1012. struct pci_controller *hose = dev_get_uclass_priv(ctlr);
  1013. struct pci_region *res;
  1014. int i;
  1015. if (hose->region_count == 0) {
  1016. *pa = bus_addr;
  1017. return 0;
  1018. }
  1019. for (i = 0; i < hose->region_count; i++) {
  1020. res = &hose->regions[i];
  1021. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  1022. continue;
  1023. if (res->flags & skip_mask)
  1024. continue;
  1025. if (bus_addr >= res->bus_start &&
  1026. (bus_addr - res->bus_start) < res->size) {
  1027. *pa = (bus_addr - res->bus_start + res->phys_start);
  1028. return 0;
  1029. }
  1030. }
  1031. return 1;
  1032. }
  1033. phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
  1034. unsigned long flags)
  1035. {
  1036. phys_addr_t phys_addr = 0;
  1037. struct udevice *ctlr;
  1038. int ret;
  1039. /* The root controller has the region information */
  1040. ctlr = pci_get_controller(dev);
  1041. /*
  1042. * if PCI_REGION_MEM is set we do a two pass search with preference
  1043. * on matches that don't have PCI_REGION_SYS_MEMORY set
  1044. */
  1045. if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
  1046. ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
  1047. flags, PCI_REGION_SYS_MEMORY,
  1048. &phys_addr);
  1049. if (!ret)
  1050. return phys_addr;
  1051. }
  1052. ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
  1053. if (ret)
  1054. puts("pci_hose_bus_to_phys: invalid physical address\n");
  1055. return phys_addr;
  1056. }
  1057. int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
  1058. unsigned long flags, unsigned long skip_mask,
  1059. pci_addr_t *ba)
  1060. {
  1061. struct pci_region *res;
  1062. struct udevice *ctlr;
  1063. pci_addr_t bus_addr;
  1064. int i;
  1065. struct pci_controller *hose;
  1066. /* The root controller has the region information */
  1067. ctlr = pci_get_controller(dev);
  1068. hose = dev_get_uclass_priv(ctlr);
  1069. if (hose->region_count == 0) {
  1070. *ba = phys_addr;
  1071. return 0;
  1072. }
  1073. for (i = 0; i < hose->region_count; i++) {
  1074. res = &hose->regions[i];
  1075. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  1076. continue;
  1077. if (res->flags & skip_mask)
  1078. continue;
  1079. bus_addr = phys_addr - res->phys_start + res->bus_start;
  1080. if (bus_addr >= res->bus_start &&
  1081. (bus_addr - res->bus_start) < res->size) {
  1082. *ba = bus_addr;
  1083. return 0;
  1084. }
  1085. }
  1086. return 1;
  1087. }
  1088. pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
  1089. unsigned long flags)
  1090. {
  1091. pci_addr_t bus_addr = 0;
  1092. int ret;
  1093. /*
  1094. * if PCI_REGION_MEM is set we do a two pass search with preference
  1095. * on matches that don't have PCI_REGION_SYS_MEMORY set
  1096. */
  1097. if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
  1098. ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
  1099. PCI_REGION_SYS_MEMORY, &bus_addr);
  1100. if (!ret)
  1101. return bus_addr;
  1102. }
  1103. ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
  1104. if (ret)
  1105. puts("pci_hose_phys_to_bus: invalid physical address\n");
  1106. return bus_addr;
  1107. }
  1108. void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
  1109. {
  1110. pci_addr_t pci_bus_addr;
  1111. u32 bar_response;
  1112. /* read BAR address */
  1113. dm_pci_read_config32(dev, bar, &bar_response);
  1114. pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
  1115. /*
  1116. * Pass "0" as the length argument to pci_bus_to_virt. The arg
  1117. * isn't actualy used on any platform because u-boot assumes a static
  1118. * linear mapping. In the future, this could read the BAR size
  1119. * and pass that as the size if needed.
  1120. */
  1121. return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
  1122. }
  1123. int dm_pci_find_capability(struct udevice *dev, int cap)
  1124. {
  1125. u16 status;
  1126. u8 header_type;
  1127. int ttl = PCI_FIND_CAP_TTL;
  1128. u8 id;
  1129. u16 ent;
  1130. u8 pos;
  1131. dm_pci_read_config16(dev, PCI_STATUS, &status);
  1132. if (!(status & PCI_STATUS_CAP_LIST))
  1133. return 0;
  1134. dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
  1135. if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
  1136. pos = PCI_CB_CAPABILITY_LIST;
  1137. else
  1138. pos = PCI_CAPABILITY_LIST;
  1139. dm_pci_read_config8(dev, pos, &pos);
  1140. while (ttl--) {
  1141. if (pos < PCI_STD_HEADER_SIZEOF)
  1142. break;
  1143. pos &= ~3;
  1144. dm_pci_read_config16(dev, pos, &ent);
  1145. id = ent & 0xff;
  1146. if (id == 0xff)
  1147. break;
  1148. if (id == cap)
  1149. return pos;
  1150. pos = (ent >> 8);
  1151. }
  1152. return 0;
  1153. }
  1154. int dm_pci_find_ext_capability(struct udevice *dev, int cap)
  1155. {
  1156. u32 header;
  1157. int ttl;
  1158. int pos = PCI_CFG_SPACE_SIZE;
  1159. /* minimum 8 bytes per capability */
  1160. ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
  1161. dm_pci_read_config32(dev, pos, &header);
  1162. /*
  1163. * If we have no capabilities, this is indicated by cap ID,
  1164. * cap version and next pointer all being 0.
  1165. */
  1166. if (header == 0)
  1167. return 0;
  1168. while (ttl--) {
  1169. if (PCI_EXT_CAP_ID(header) == cap)
  1170. return pos;
  1171. pos = PCI_EXT_CAP_NEXT(header);
  1172. if (pos < PCI_CFG_SPACE_SIZE)
  1173. break;
  1174. dm_pci_read_config32(dev, pos, &header);
  1175. }
  1176. return 0;
  1177. }
  1178. UCLASS_DRIVER(pci) = {
  1179. .id = UCLASS_PCI,
  1180. .name = "pci",
  1181. .flags = DM_UC_FLAG_SEQ_ALIAS,
  1182. .post_bind = dm_scan_fdt_dev,
  1183. .pre_probe = pci_uclass_pre_probe,
  1184. .post_probe = pci_uclass_post_probe,
  1185. .child_post_bind = pci_uclass_child_post_bind,
  1186. .per_device_auto_alloc_size = sizeof(struct pci_controller),
  1187. .per_child_platdata_auto_alloc_size =
  1188. sizeof(struct pci_child_platdata),
  1189. };
  1190. static const struct dm_pci_ops pci_bridge_ops = {
  1191. .read_config = pci_bridge_read_config,
  1192. .write_config = pci_bridge_write_config,
  1193. };
  1194. static const struct udevice_id pci_bridge_ids[] = {
  1195. { .compatible = "pci-bridge" },
  1196. { }
  1197. };
  1198. U_BOOT_DRIVER(pci_bridge_drv) = {
  1199. .name = "pci_bridge_drv",
  1200. .id = UCLASS_PCI,
  1201. .of_match = pci_bridge_ids,
  1202. .ops = &pci_bridge_ops,
  1203. };
  1204. UCLASS_DRIVER(pci_generic) = {
  1205. .id = UCLASS_PCI_GENERIC,
  1206. .name = "pci_generic",
  1207. };
  1208. static const struct udevice_id pci_generic_ids[] = {
  1209. { .compatible = "pci-generic" },
  1210. { }
  1211. };
  1212. U_BOOT_DRIVER(pci_generic_drv) = {
  1213. .name = "pci_generic_drv",
  1214. .id = UCLASS_PCI_GENERIC,
  1215. .of_match = pci_generic_ids,
  1216. };
  1217. void pci_init(void)
  1218. {
  1219. struct udevice *bus;
  1220. /*
  1221. * Enumerate all known controller devices. Enumeration has the side-
  1222. * effect of probing them, so PCIe devices will be enumerated too.
  1223. */
  1224. for (uclass_first_device(UCLASS_PCI, &bus);
  1225. bus;
  1226. uclass_next_device(&bus)) {
  1227. ;
  1228. }
  1229. }