pci-uclass.c 30 KB

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