pci-uclass.c 26 KB

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