pci-uclass.c 28 KB

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