pci-uclass.c 29 KB

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