pci-uclass.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. * @find: the PCI device id structure to match against
  451. *
  452. * Returns true if the finding pci_device_id structure matched or false if
  453. * there is no match.
  454. */
  455. static bool pci_match_one_id(const struct pci_device_id *id,
  456. const struct pci_device_id *find)
  457. {
  458. if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
  459. (id->device == PCI_ANY_ID || id->device == find->device) &&
  460. (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
  461. (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
  462. !((id->class ^ find->class) & id->class_mask))
  463. return true;
  464. return false;
  465. }
  466. /**
  467. * pci_find_and_bind_driver() - Find and bind the right PCI driver
  468. *
  469. * This only looks at certain fields in the descriptor.
  470. *
  471. * @parent: Parent bus
  472. * @find_id: Specification of the driver to find
  473. * @bdf: Bus/device/function addreess - see PCI_BDF()
  474. * @devp: Returns a pointer to the device created
  475. * @return 0 if OK, -EPERM if the device is not needed before relocation and
  476. * therefore was not created, other -ve value on error
  477. */
  478. static int pci_find_and_bind_driver(struct udevice *parent,
  479. struct pci_device_id *find_id,
  480. pci_dev_t bdf, struct udevice **devp)
  481. {
  482. struct pci_driver_entry *start, *entry;
  483. const char *drv;
  484. int n_ents;
  485. int ret;
  486. char name[30], *str;
  487. bool bridge;
  488. *devp = NULL;
  489. debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
  490. find_id->vendor, find_id->device);
  491. start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
  492. n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
  493. for (entry = start; entry != start + n_ents; entry++) {
  494. const struct pci_device_id *id;
  495. struct udevice *dev;
  496. const struct driver *drv;
  497. for (id = entry->match;
  498. id->vendor || id->subvendor || id->class_mask;
  499. id++) {
  500. if (!pci_match_one_id(id, find_id))
  501. continue;
  502. drv = entry->driver;
  503. /*
  504. * In the pre-relocation phase, we only bind devices
  505. * whose driver has the DM_FLAG_PRE_RELOC set, to save
  506. * precious memory space as on some platforms as that
  507. * space is pretty limited (ie: using Cache As RAM).
  508. */
  509. if (!(gd->flags & GD_FLG_RELOC) &&
  510. !(drv->flags & DM_FLAG_PRE_RELOC))
  511. return -EPERM;
  512. /*
  513. * We could pass the descriptor to the driver as
  514. * platdata (instead of NULL) and allow its bind()
  515. * method to return -ENOENT if it doesn't support this
  516. * device. That way we could continue the search to
  517. * find another driver. For now this doesn't seem
  518. * necesssary, so just bind the first match.
  519. */
  520. ret = device_bind(parent, drv, drv->name, NULL, -1,
  521. &dev);
  522. if (ret)
  523. goto error;
  524. debug("%s: Match found: %s\n", __func__, drv->name);
  525. dev->driver_data = find_id->driver_data;
  526. *devp = dev;
  527. return 0;
  528. }
  529. }
  530. bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
  531. /*
  532. * In the pre-relocation phase, we only bind bridge devices to save
  533. * precious memory space as on some platforms as that space is pretty
  534. * limited (ie: using Cache As RAM).
  535. */
  536. if (!(gd->flags & GD_FLG_RELOC) && !bridge)
  537. return -EPERM;
  538. /* Bind a generic driver so that the device can be used */
  539. sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
  540. PCI_FUNC(bdf));
  541. str = strdup(name);
  542. if (!str)
  543. return -ENOMEM;
  544. drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
  545. ret = device_bind_driver(parent, drv, str, devp);
  546. if (ret) {
  547. debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
  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, const void *blob,
  628. int parent_node, int 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 = fdt_getprop(blob, node, "ranges", &len);
  637. if (!prop)
  638. return -EINVAL;
  639. pci_addr_cells = fdt_address_cells(blob, node);
  640. addr_cells = fdt_address_cells(blob, parent_node);
  641. size_cells = fdt_size_cells(blob, 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, gd->fdt_blob,
  707. dev_of_offset(bus->parent),
  708. dev_of_offset(bus));
  709. if (ret) {
  710. debug("%s: Cannot decode regions\n", __func__);
  711. return ret;
  712. }
  713. } else {
  714. struct pci_controller *parent_hose;
  715. parent_hose = dev_get_uclass_priv(bus->parent);
  716. hose->ctlr = parent_hose->bus;
  717. }
  718. hose->bus = bus;
  719. hose->first_busno = bus->seq;
  720. hose->last_busno = bus->seq;
  721. return 0;
  722. }
  723. static int pci_uclass_post_probe(struct udevice *bus)
  724. {
  725. int ret;
  726. debug("%s: probing bus %d\n", __func__, bus->seq);
  727. ret = pci_bind_bus_devices(bus);
  728. if (ret)
  729. return ret;
  730. #ifdef CONFIG_PCI_PNP
  731. ret = pci_auto_config_devices(bus);
  732. if (ret < 0)
  733. return ret;
  734. #endif
  735. #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
  736. /*
  737. * Per Intel FSP specification, we should call FSP notify API to
  738. * inform FSP that PCI enumeration has been done so that FSP will
  739. * do any necessary initialization as required by the chipset's
  740. * BIOS Writer's Guide (BWG).
  741. *
  742. * Unfortunately we have to put this call here as with driver model,
  743. * the enumeration is all done on a lazy basis as needed, so until
  744. * something is touched on PCI it won't happen.
  745. *
  746. * Note we only call this 1) after U-Boot is relocated, and 2)
  747. * root bus has finished probing.
  748. */
  749. if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
  750. ret = fsp_init_phase_pci();
  751. if (ret)
  752. return ret;
  753. }
  754. #endif
  755. return 0;
  756. }
  757. static int pci_uclass_child_post_bind(struct udevice *dev)
  758. {
  759. struct pci_child_platdata *pplat;
  760. struct fdt_pci_addr addr;
  761. int ret;
  762. if (dev_of_offset(dev) == -1)
  763. return 0;
  764. /*
  765. * We could read vendor, device, class if available. But for now we
  766. * just check the address.
  767. */
  768. pplat = dev_get_parent_platdata(dev);
  769. ret = fdtdec_get_pci_addr(gd->fdt_blob, dev_of_offset(dev),
  770. FDT_PCI_SPACE_CONFIG, "reg", &addr);
  771. if (ret) {
  772. if (ret != -ENOENT)
  773. return -EINVAL;
  774. } else {
  775. /* extract the devfn from fdt_pci_addr */
  776. pplat->devfn = addr.phys_hi & 0xff00;
  777. }
  778. return 0;
  779. }
  780. static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
  781. uint offset, ulong *valuep,
  782. enum pci_size_t size)
  783. {
  784. struct pci_controller *hose = bus->uclass_priv;
  785. return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
  786. }
  787. static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
  788. uint offset, ulong value,
  789. enum pci_size_t size)
  790. {
  791. struct pci_controller *hose = bus->uclass_priv;
  792. return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
  793. }
  794. static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
  795. {
  796. struct udevice *dev;
  797. int ret = 0;
  798. /*
  799. * Scan through all the PCI controllers. On x86 there will only be one
  800. * but that is not necessarily true on other hardware.
  801. */
  802. do {
  803. device_find_first_child(bus, &dev);
  804. if (dev) {
  805. *devp = dev;
  806. return 0;
  807. }
  808. ret = uclass_next_device(&bus);
  809. if (ret)
  810. return ret;
  811. } while (bus);
  812. return 0;
  813. }
  814. int pci_find_next_device(struct udevice **devp)
  815. {
  816. struct udevice *child = *devp;
  817. struct udevice *bus = child->parent;
  818. int ret;
  819. /* First try all the siblings */
  820. *devp = NULL;
  821. while (child) {
  822. device_find_next_child(&child);
  823. if (child) {
  824. *devp = child;
  825. return 0;
  826. }
  827. }
  828. /* We ran out of siblings. Try the next bus */
  829. ret = uclass_next_device(&bus);
  830. if (ret)
  831. return ret;
  832. return bus ? skip_to_next_device(bus, devp) : 0;
  833. }
  834. int pci_find_first_device(struct udevice **devp)
  835. {
  836. struct udevice *bus;
  837. int ret;
  838. *devp = NULL;
  839. ret = uclass_first_device(UCLASS_PCI, &bus);
  840. if (ret)
  841. return ret;
  842. return skip_to_next_device(bus, devp);
  843. }
  844. ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
  845. {
  846. switch (size) {
  847. case PCI_SIZE_8:
  848. return (value >> ((offset & 3) * 8)) & 0xff;
  849. case PCI_SIZE_16:
  850. return (value >> ((offset & 2) * 8)) & 0xffff;
  851. default:
  852. return value;
  853. }
  854. }
  855. ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
  856. enum pci_size_t size)
  857. {
  858. uint off_mask;
  859. uint val_mask, shift;
  860. ulong ldata, mask;
  861. switch (size) {
  862. case PCI_SIZE_8:
  863. off_mask = 3;
  864. val_mask = 0xff;
  865. break;
  866. case PCI_SIZE_16:
  867. off_mask = 2;
  868. val_mask = 0xffff;
  869. break;
  870. default:
  871. return value;
  872. }
  873. shift = (offset & off_mask) * 8;
  874. ldata = (value & val_mask) << shift;
  875. mask = val_mask << shift;
  876. value = (old & ~mask) | ldata;
  877. return value;
  878. }
  879. int pci_get_regions(struct udevice *dev, struct pci_region **iop,
  880. struct pci_region **memp, struct pci_region **prefp)
  881. {
  882. struct udevice *bus = pci_get_controller(dev);
  883. struct pci_controller *hose = dev_get_uclass_priv(bus);
  884. int i;
  885. *iop = NULL;
  886. *memp = NULL;
  887. *prefp = NULL;
  888. for (i = 0; i < hose->region_count; i++) {
  889. switch (hose->regions[i].flags) {
  890. case PCI_REGION_IO:
  891. if (!*iop || (*iop)->size < hose->regions[i].size)
  892. *iop = hose->regions + i;
  893. break;
  894. case PCI_REGION_MEM:
  895. if (!*memp || (*memp)->size < hose->regions[i].size)
  896. *memp = hose->regions + i;
  897. break;
  898. case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
  899. if (!*prefp || (*prefp)->size < hose->regions[i].size)
  900. *prefp = hose->regions + i;
  901. break;
  902. }
  903. }
  904. return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
  905. }
  906. u32 dm_pci_read_bar32(struct udevice *dev, int barnum)
  907. {
  908. u32 addr;
  909. int bar;
  910. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  911. dm_pci_read_config32(dev, bar, &addr);
  912. if (addr & PCI_BASE_ADDRESS_SPACE_IO)
  913. return addr & PCI_BASE_ADDRESS_IO_MASK;
  914. else
  915. return addr & PCI_BASE_ADDRESS_MEM_MASK;
  916. }
  917. void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
  918. {
  919. int bar;
  920. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  921. dm_pci_write_config32(dev, bar, addr);
  922. }
  923. static int _dm_pci_bus_to_phys(struct udevice *ctlr,
  924. pci_addr_t bus_addr, unsigned long flags,
  925. unsigned long skip_mask, phys_addr_t *pa)
  926. {
  927. struct pci_controller *hose = dev_get_uclass_priv(ctlr);
  928. struct pci_region *res;
  929. int i;
  930. for (i = 0; i < hose->region_count; i++) {
  931. res = &hose->regions[i];
  932. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  933. continue;
  934. if (res->flags & skip_mask)
  935. continue;
  936. if (bus_addr >= res->bus_start &&
  937. (bus_addr - res->bus_start) < res->size) {
  938. *pa = (bus_addr - res->bus_start + res->phys_start);
  939. return 0;
  940. }
  941. }
  942. return 1;
  943. }
  944. phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
  945. unsigned long flags)
  946. {
  947. phys_addr_t phys_addr = 0;
  948. struct udevice *ctlr;
  949. int ret;
  950. /* The root controller has the region information */
  951. ctlr = pci_get_controller(dev);
  952. /*
  953. * if PCI_REGION_MEM is set we do a two pass search with preference
  954. * on matches that don't have PCI_REGION_SYS_MEMORY set
  955. */
  956. if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
  957. ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
  958. flags, PCI_REGION_SYS_MEMORY,
  959. &phys_addr);
  960. if (!ret)
  961. return phys_addr;
  962. }
  963. ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
  964. if (ret)
  965. puts("pci_hose_bus_to_phys: invalid physical address\n");
  966. return phys_addr;
  967. }
  968. int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
  969. unsigned long flags, unsigned long skip_mask,
  970. pci_addr_t *ba)
  971. {
  972. struct pci_region *res;
  973. struct udevice *ctlr;
  974. pci_addr_t bus_addr;
  975. int i;
  976. struct pci_controller *hose;
  977. /* The root controller has the region information */
  978. ctlr = pci_get_controller(dev);
  979. hose = dev_get_uclass_priv(ctlr);
  980. for (i = 0; i < hose->region_count; i++) {
  981. res = &hose->regions[i];
  982. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  983. continue;
  984. if (res->flags & skip_mask)
  985. continue;
  986. bus_addr = phys_addr - res->phys_start + res->bus_start;
  987. if (bus_addr >= res->bus_start &&
  988. (bus_addr - res->bus_start) < res->size) {
  989. *ba = bus_addr;
  990. return 0;
  991. }
  992. }
  993. return 1;
  994. }
  995. pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
  996. unsigned long flags)
  997. {
  998. pci_addr_t bus_addr = 0;
  999. int ret;
  1000. /*
  1001. * if PCI_REGION_MEM is set we do a two pass search with preference
  1002. * on matches that don't have PCI_REGION_SYS_MEMORY set
  1003. */
  1004. if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
  1005. ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
  1006. PCI_REGION_SYS_MEMORY, &bus_addr);
  1007. if (!ret)
  1008. return bus_addr;
  1009. }
  1010. ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
  1011. if (ret)
  1012. puts("pci_hose_phys_to_bus: invalid physical address\n");
  1013. return bus_addr;
  1014. }
  1015. void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
  1016. {
  1017. pci_addr_t pci_bus_addr;
  1018. u32 bar_response;
  1019. /* read BAR address */
  1020. dm_pci_read_config32(dev, bar, &bar_response);
  1021. pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
  1022. /*
  1023. * Pass "0" as the length argument to pci_bus_to_virt. The arg
  1024. * isn't actualy used on any platform because u-boot assumes a static
  1025. * linear mapping. In the future, this could read the BAR size
  1026. * and pass that as the size if needed.
  1027. */
  1028. return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
  1029. }
  1030. UCLASS_DRIVER(pci) = {
  1031. .id = UCLASS_PCI,
  1032. .name = "pci",
  1033. .flags = DM_UC_FLAG_SEQ_ALIAS,
  1034. .post_bind = dm_scan_fdt_dev,
  1035. .pre_probe = pci_uclass_pre_probe,
  1036. .post_probe = pci_uclass_post_probe,
  1037. .child_post_bind = pci_uclass_child_post_bind,
  1038. .per_device_auto_alloc_size = sizeof(struct pci_controller),
  1039. .per_child_platdata_auto_alloc_size =
  1040. sizeof(struct pci_child_platdata),
  1041. };
  1042. static const struct dm_pci_ops pci_bridge_ops = {
  1043. .read_config = pci_bridge_read_config,
  1044. .write_config = pci_bridge_write_config,
  1045. };
  1046. static const struct udevice_id pci_bridge_ids[] = {
  1047. { .compatible = "pci-bridge" },
  1048. { }
  1049. };
  1050. U_BOOT_DRIVER(pci_bridge_drv) = {
  1051. .name = "pci_bridge_drv",
  1052. .id = UCLASS_PCI,
  1053. .of_match = pci_bridge_ids,
  1054. .ops = &pci_bridge_ops,
  1055. };
  1056. UCLASS_DRIVER(pci_generic) = {
  1057. .id = UCLASS_PCI_GENERIC,
  1058. .name = "pci_generic",
  1059. };
  1060. static const struct udevice_id pci_generic_ids[] = {
  1061. { .compatible = "pci-generic" },
  1062. { }
  1063. };
  1064. U_BOOT_DRIVER(pci_generic_drv) = {
  1065. .name = "pci_generic_drv",
  1066. .id = UCLASS_PCI_GENERIC,
  1067. .of_match = pci_generic_ids,
  1068. };
  1069. void pci_init(void)
  1070. {
  1071. struct udevice *bus;
  1072. /*
  1073. * Enumerate all known controller devices. Enumeration has the side-
  1074. * effect of probing them, so PCIe devices will be enumerated too.
  1075. */
  1076. for (uclass_first_device(UCLASS_PCI, &bus);
  1077. bus;
  1078. uclass_next_device(&bus)) {
  1079. ;
  1080. }
  1081. }