pci-uclass.c 32 KB

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