pci-uclass.c 29 KB

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