pci-uclass.c 30 KB

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