pci-uclass.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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. int cells_per_record;
  684. const u32 *prop;
  685. int len;
  686. int i;
  687. prop = ofnode_get_property(node, "ranges", &len);
  688. if (!prop)
  689. return -EINVAL;
  690. pci_addr_cells = ofnode_read_simple_addr_cells(node);
  691. addr_cells = ofnode_read_simple_addr_cells(parent_node);
  692. size_cells = ofnode_read_simple_size_cells(node);
  693. /* PCI addresses are always 3-cells */
  694. len /= sizeof(u32);
  695. cells_per_record = pci_addr_cells + addr_cells + size_cells;
  696. hose->region_count = 0;
  697. debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
  698. cells_per_record);
  699. for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
  700. u64 pci_addr, addr, size;
  701. int space_code;
  702. u32 flags;
  703. int type;
  704. int pos;
  705. if (len < cells_per_record)
  706. break;
  707. flags = fdt32_to_cpu(prop[0]);
  708. space_code = (flags >> 24) & 3;
  709. pci_addr = fdtdec_get_number(prop + 1, 2);
  710. prop += pci_addr_cells;
  711. addr = fdtdec_get_number(prop, addr_cells);
  712. prop += addr_cells;
  713. size = fdtdec_get_number(prop, size_cells);
  714. prop += size_cells;
  715. debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
  716. ", size=%" PRIx64 ", space_code=%d\n", __func__,
  717. hose->region_count, pci_addr, addr, size, space_code);
  718. if (space_code & 2) {
  719. type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
  720. PCI_REGION_MEM;
  721. } else if (space_code & 1) {
  722. type = PCI_REGION_IO;
  723. } else {
  724. continue;
  725. }
  726. pos = -1;
  727. for (i = 0; i < hose->region_count; i++) {
  728. if (hose->regions[i].flags == type)
  729. pos = i;
  730. }
  731. if (pos == -1)
  732. pos = hose->region_count++;
  733. debug(" - type=%d, pos=%d\n", type, pos);
  734. pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
  735. }
  736. /* Add a region for our local memory */
  737. #ifdef CONFIG_NR_DRAM_BANKS
  738. bd_t *bd = gd->bd;
  739. for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
  740. if (bd->bi_dram[i].size) {
  741. pci_set_region(hose->regions + hose->region_count++,
  742. bd->bi_dram[i].start,
  743. bd->bi_dram[i].start,
  744. bd->bi_dram[i].size,
  745. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  746. }
  747. }
  748. #else
  749. phys_addr_t base = 0, size;
  750. size = gd->ram_size;
  751. #ifdef CONFIG_SYS_SDRAM_BASE
  752. base = CONFIG_SYS_SDRAM_BASE;
  753. #endif
  754. if (gd->pci_ram_top && gd->pci_ram_top < base + size)
  755. size = gd->pci_ram_top - base;
  756. pci_set_region(hose->regions + hose->region_count++, base, base,
  757. size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  758. #endif
  759. return 0;
  760. }
  761. static int pci_uclass_pre_probe(struct udevice *bus)
  762. {
  763. struct pci_controller *hose;
  764. int ret;
  765. debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
  766. bus->parent->name);
  767. hose = bus->uclass_priv;
  768. /* For bridges, use the top-level PCI controller */
  769. if (!device_is_on_pci_bus(bus)) {
  770. hose->ctlr = bus;
  771. ret = decode_regions(hose, dev_ofnode(bus->parent),
  772. dev_ofnode(bus));
  773. if (ret) {
  774. debug("%s: Cannot decode regions\n", __func__);
  775. return ret;
  776. }
  777. } else {
  778. struct pci_controller *parent_hose;
  779. parent_hose = dev_get_uclass_priv(bus->parent);
  780. hose->ctlr = parent_hose->bus;
  781. }
  782. hose->bus = bus;
  783. hose->first_busno = bus->seq;
  784. hose->last_busno = bus->seq;
  785. return 0;
  786. }
  787. static int pci_uclass_post_probe(struct udevice *bus)
  788. {
  789. int ret;
  790. debug("%s: probing bus %d\n", __func__, bus->seq);
  791. ret = pci_bind_bus_devices(bus);
  792. if (ret)
  793. return ret;
  794. #ifdef CONFIG_PCI_PNP
  795. ret = pci_auto_config_devices(bus);
  796. if (ret < 0)
  797. return ret;
  798. #endif
  799. #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
  800. /*
  801. * Per Intel FSP specification, we should call FSP notify API to
  802. * inform FSP that PCI enumeration has been done so that FSP will
  803. * do any necessary initialization as required by the chipset's
  804. * BIOS Writer's Guide (BWG).
  805. *
  806. * Unfortunately we have to put this call here as with driver model,
  807. * the enumeration is all done on a lazy basis as needed, so until
  808. * something is touched on PCI it won't happen.
  809. *
  810. * Note we only call this 1) after U-Boot is relocated, and 2)
  811. * root bus has finished probing.
  812. */
  813. if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
  814. ret = fsp_init_phase_pci();
  815. if (ret)
  816. return ret;
  817. }
  818. #endif
  819. return 0;
  820. }
  821. static int pci_uclass_child_post_bind(struct udevice *dev)
  822. {
  823. struct pci_child_platdata *pplat;
  824. struct fdt_pci_addr addr;
  825. int ret;
  826. if (!dev_of_valid(dev))
  827. return 0;
  828. /*
  829. * We could read vendor, device, class if available. But for now we
  830. * just check the address.
  831. */
  832. pplat = dev_get_parent_platdata(dev);
  833. ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_CONFIG, "reg",
  834. &addr);
  835. if (ret) {
  836. if (ret != -ENOENT)
  837. return -EINVAL;
  838. } else {
  839. /* extract the devfn from fdt_pci_addr */
  840. pplat->devfn = addr.phys_hi & 0xff00;
  841. }
  842. return 0;
  843. }
  844. static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
  845. uint offset, ulong *valuep,
  846. enum pci_size_t size)
  847. {
  848. struct pci_controller *hose = bus->uclass_priv;
  849. return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
  850. }
  851. static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
  852. uint offset, ulong value,
  853. enum pci_size_t size)
  854. {
  855. struct pci_controller *hose = bus->uclass_priv;
  856. return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
  857. }
  858. static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
  859. {
  860. struct udevice *dev;
  861. int ret = 0;
  862. /*
  863. * Scan through all the PCI controllers. On x86 there will only be one
  864. * but that is not necessarily true on other hardware.
  865. */
  866. do {
  867. device_find_first_child(bus, &dev);
  868. if (dev) {
  869. *devp = dev;
  870. return 0;
  871. }
  872. ret = uclass_next_device(&bus);
  873. if (ret)
  874. return ret;
  875. } while (bus);
  876. return 0;
  877. }
  878. int pci_find_next_device(struct udevice **devp)
  879. {
  880. struct udevice *child = *devp;
  881. struct udevice *bus = child->parent;
  882. int ret;
  883. /* First try all the siblings */
  884. *devp = NULL;
  885. while (child) {
  886. device_find_next_child(&child);
  887. if (child) {
  888. *devp = child;
  889. return 0;
  890. }
  891. }
  892. /* We ran out of siblings. Try the next bus */
  893. ret = uclass_next_device(&bus);
  894. if (ret)
  895. return ret;
  896. return bus ? skip_to_next_device(bus, devp) : 0;
  897. }
  898. int pci_find_first_device(struct udevice **devp)
  899. {
  900. struct udevice *bus;
  901. int ret;
  902. *devp = NULL;
  903. ret = uclass_first_device(UCLASS_PCI, &bus);
  904. if (ret)
  905. return ret;
  906. return skip_to_next_device(bus, devp);
  907. }
  908. ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
  909. {
  910. switch (size) {
  911. case PCI_SIZE_8:
  912. return (value >> ((offset & 3) * 8)) & 0xff;
  913. case PCI_SIZE_16:
  914. return (value >> ((offset & 2) * 8)) & 0xffff;
  915. default:
  916. return value;
  917. }
  918. }
  919. ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
  920. enum pci_size_t size)
  921. {
  922. uint off_mask;
  923. uint val_mask, shift;
  924. ulong ldata, mask;
  925. switch (size) {
  926. case PCI_SIZE_8:
  927. off_mask = 3;
  928. val_mask = 0xff;
  929. break;
  930. case PCI_SIZE_16:
  931. off_mask = 2;
  932. val_mask = 0xffff;
  933. break;
  934. default:
  935. return value;
  936. }
  937. shift = (offset & off_mask) * 8;
  938. ldata = (value & val_mask) << shift;
  939. mask = val_mask << shift;
  940. value = (old & ~mask) | ldata;
  941. return value;
  942. }
  943. int pci_get_regions(struct udevice *dev, struct pci_region **iop,
  944. struct pci_region **memp, struct pci_region **prefp)
  945. {
  946. struct udevice *bus = pci_get_controller(dev);
  947. struct pci_controller *hose = dev_get_uclass_priv(bus);
  948. int i;
  949. *iop = NULL;
  950. *memp = NULL;
  951. *prefp = NULL;
  952. for (i = 0; i < hose->region_count; i++) {
  953. switch (hose->regions[i].flags) {
  954. case PCI_REGION_IO:
  955. if (!*iop || (*iop)->size < hose->regions[i].size)
  956. *iop = hose->regions + i;
  957. break;
  958. case PCI_REGION_MEM:
  959. if (!*memp || (*memp)->size < hose->regions[i].size)
  960. *memp = hose->regions + i;
  961. break;
  962. case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
  963. if (!*prefp || (*prefp)->size < hose->regions[i].size)
  964. *prefp = hose->regions + i;
  965. break;
  966. }
  967. }
  968. return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
  969. }
  970. u32 dm_pci_read_bar32(struct udevice *dev, int barnum)
  971. {
  972. u32 addr;
  973. int bar;
  974. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  975. dm_pci_read_config32(dev, bar, &addr);
  976. if (addr & PCI_BASE_ADDRESS_SPACE_IO)
  977. return addr & PCI_BASE_ADDRESS_IO_MASK;
  978. else
  979. return addr & PCI_BASE_ADDRESS_MEM_MASK;
  980. }
  981. void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
  982. {
  983. int bar;
  984. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  985. dm_pci_write_config32(dev, bar, addr);
  986. }
  987. static int _dm_pci_bus_to_phys(struct udevice *ctlr,
  988. pci_addr_t bus_addr, unsigned long flags,
  989. unsigned long skip_mask, phys_addr_t *pa)
  990. {
  991. struct pci_controller *hose = dev_get_uclass_priv(ctlr);
  992. struct pci_region *res;
  993. int i;
  994. for (i = 0; i < hose->region_count; i++) {
  995. res = &hose->regions[i];
  996. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  997. continue;
  998. if (res->flags & skip_mask)
  999. continue;
  1000. if (bus_addr >= res->bus_start &&
  1001. (bus_addr - res->bus_start) < res->size) {
  1002. *pa = (bus_addr - res->bus_start + res->phys_start);
  1003. return 0;
  1004. }
  1005. }
  1006. return 1;
  1007. }
  1008. phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
  1009. unsigned long flags)
  1010. {
  1011. phys_addr_t phys_addr = 0;
  1012. struct udevice *ctlr;
  1013. int ret;
  1014. /* The root controller has the region information */
  1015. ctlr = pci_get_controller(dev);
  1016. /*
  1017. * if PCI_REGION_MEM is set we do a two pass search with preference
  1018. * on matches that don't have PCI_REGION_SYS_MEMORY set
  1019. */
  1020. if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
  1021. ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
  1022. flags, PCI_REGION_SYS_MEMORY,
  1023. &phys_addr);
  1024. if (!ret)
  1025. return phys_addr;
  1026. }
  1027. ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
  1028. if (ret)
  1029. puts("pci_hose_bus_to_phys: invalid physical address\n");
  1030. return phys_addr;
  1031. }
  1032. int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
  1033. unsigned long flags, unsigned long skip_mask,
  1034. pci_addr_t *ba)
  1035. {
  1036. struct pci_region *res;
  1037. struct udevice *ctlr;
  1038. pci_addr_t bus_addr;
  1039. int i;
  1040. struct pci_controller *hose;
  1041. /* The root controller has the region information */
  1042. ctlr = pci_get_controller(dev);
  1043. hose = dev_get_uclass_priv(ctlr);
  1044. for (i = 0; i < hose->region_count; i++) {
  1045. res = &hose->regions[i];
  1046. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  1047. continue;
  1048. if (res->flags & skip_mask)
  1049. continue;
  1050. bus_addr = phys_addr - res->phys_start + res->bus_start;
  1051. if (bus_addr >= res->bus_start &&
  1052. (bus_addr - res->bus_start) < res->size) {
  1053. *ba = bus_addr;
  1054. return 0;
  1055. }
  1056. }
  1057. return 1;
  1058. }
  1059. pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
  1060. unsigned long flags)
  1061. {
  1062. pci_addr_t bus_addr = 0;
  1063. int ret;
  1064. /*
  1065. * if PCI_REGION_MEM is set we do a two pass search with preference
  1066. * on matches that don't have PCI_REGION_SYS_MEMORY set
  1067. */
  1068. if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
  1069. ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
  1070. PCI_REGION_SYS_MEMORY, &bus_addr);
  1071. if (!ret)
  1072. return bus_addr;
  1073. }
  1074. ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
  1075. if (ret)
  1076. puts("pci_hose_phys_to_bus: invalid physical address\n");
  1077. return bus_addr;
  1078. }
  1079. void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
  1080. {
  1081. pci_addr_t pci_bus_addr;
  1082. u32 bar_response;
  1083. /* read BAR address */
  1084. dm_pci_read_config32(dev, bar, &bar_response);
  1085. pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
  1086. /*
  1087. * Pass "0" as the length argument to pci_bus_to_virt. The arg
  1088. * isn't actualy used on any platform because u-boot assumes a static
  1089. * linear mapping. In the future, this could read the BAR size
  1090. * and pass that as the size if needed.
  1091. */
  1092. return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
  1093. }
  1094. UCLASS_DRIVER(pci) = {
  1095. .id = UCLASS_PCI,
  1096. .name = "pci",
  1097. .flags = DM_UC_FLAG_SEQ_ALIAS,
  1098. .post_bind = dm_scan_fdt_dev,
  1099. .pre_probe = pci_uclass_pre_probe,
  1100. .post_probe = pci_uclass_post_probe,
  1101. .child_post_bind = pci_uclass_child_post_bind,
  1102. .per_device_auto_alloc_size = sizeof(struct pci_controller),
  1103. .per_child_platdata_auto_alloc_size =
  1104. sizeof(struct pci_child_platdata),
  1105. };
  1106. static const struct dm_pci_ops pci_bridge_ops = {
  1107. .read_config = pci_bridge_read_config,
  1108. .write_config = pci_bridge_write_config,
  1109. };
  1110. static const struct udevice_id pci_bridge_ids[] = {
  1111. { .compatible = "pci-bridge" },
  1112. { }
  1113. };
  1114. U_BOOT_DRIVER(pci_bridge_drv) = {
  1115. .name = "pci_bridge_drv",
  1116. .id = UCLASS_PCI,
  1117. .of_match = pci_bridge_ids,
  1118. .ops = &pci_bridge_ops,
  1119. };
  1120. UCLASS_DRIVER(pci_generic) = {
  1121. .id = UCLASS_PCI_GENERIC,
  1122. .name = "pci_generic",
  1123. };
  1124. static const struct udevice_id pci_generic_ids[] = {
  1125. { .compatible = "pci-generic" },
  1126. { }
  1127. };
  1128. U_BOOT_DRIVER(pci_generic_drv) = {
  1129. .name = "pci_generic_drv",
  1130. .id = UCLASS_PCI_GENERIC,
  1131. .of_match = pci_generic_ids,
  1132. };
  1133. void pci_init(void)
  1134. {
  1135. struct udevice *bus;
  1136. /*
  1137. * Enumerate all known controller devices. Enumeration has the side-
  1138. * effect of probing them, so PCIe devices will be enumerated too.
  1139. */
  1140. for (uclass_first_device(UCLASS_PCI, &bus);
  1141. bus;
  1142. uclass_next_device(&bus)) {
  1143. ;
  1144. }
  1145. }