pci-uclass.c 29 KB

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