pci-uclass.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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. * @dev: the PCI device structure to match against
  451. *
  452. * Returns the matching pci_device_id structure or %NULL if there is no match.
  453. */
  454. static bool pci_match_one_id(const struct pci_device_id *id,
  455. const struct pci_device_id *find)
  456. {
  457. if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
  458. (id->device == PCI_ANY_ID || id->device == find->device) &&
  459. (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
  460. (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
  461. !((id->class ^ find->class) & id->class_mask))
  462. return true;
  463. return false;
  464. }
  465. /**
  466. * pci_find_and_bind_driver() - Find and bind the right PCI driver
  467. *
  468. * This only looks at certain fields in the descriptor.
  469. *
  470. * @parent: Parent bus
  471. * @find_id: Specification of the driver to find
  472. * @bdf: Bus/device/function addreess - see PCI_BDF()
  473. * @devp: Returns a pointer to the device created
  474. * @return 0 if OK, -EPERM if the device is not needed before relocation and
  475. * therefore was not created, other -ve value on error
  476. */
  477. static int pci_find_and_bind_driver(struct udevice *parent,
  478. struct pci_device_id *find_id,
  479. pci_dev_t bdf, struct udevice **devp)
  480. {
  481. struct pci_driver_entry *start, *entry;
  482. const char *drv;
  483. int n_ents;
  484. int ret;
  485. char name[30], *str;
  486. bool bridge;
  487. *devp = NULL;
  488. debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
  489. find_id->vendor, find_id->device);
  490. start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
  491. n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
  492. for (entry = start; entry != start + n_ents; entry++) {
  493. const struct pci_device_id *id;
  494. struct udevice *dev;
  495. const struct driver *drv;
  496. for (id = entry->match;
  497. id->vendor || id->subvendor || id->class_mask;
  498. id++) {
  499. if (!pci_match_one_id(id, find_id))
  500. continue;
  501. drv = entry->driver;
  502. /*
  503. * In the pre-relocation phase, we only bind devices
  504. * whose driver has the DM_FLAG_PRE_RELOC set, to save
  505. * precious memory space as on some platforms as that
  506. * space is pretty limited (ie: using Cache As RAM).
  507. */
  508. if (!(gd->flags & GD_FLG_RELOC) &&
  509. !(drv->flags & DM_FLAG_PRE_RELOC))
  510. return -EPERM;
  511. /*
  512. * We could pass the descriptor to the driver as
  513. * platdata (instead of NULL) and allow its bind()
  514. * method to return -ENOENT if it doesn't support this
  515. * device. That way we could continue the search to
  516. * find another driver. For now this doesn't seem
  517. * necesssary, so just bind the first match.
  518. */
  519. ret = device_bind(parent, drv, drv->name, NULL, -1,
  520. &dev);
  521. if (ret)
  522. goto error;
  523. debug("%s: Match found: %s\n", __func__, drv->name);
  524. dev->driver_data = find_id->driver_data;
  525. *devp = dev;
  526. return 0;
  527. }
  528. }
  529. bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
  530. /*
  531. * In the pre-relocation phase, we only bind bridge devices to save
  532. * precious memory space as on some platforms as that space is pretty
  533. * limited (ie: using Cache As RAM).
  534. */
  535. if (!(gd->flags & GD_FLG_RELOC) && !bridge)
  536. return -EPERM;
  537. /* Bind a generic driver so that the device can be used */
  538. sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
  539. PCI_FUNC(bdf));
  540. str = strdup(name);
  541. if (!str)
  542. return -ENOMEM;
  543. drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
  544. ret = device_bind_driver(parent, drv, str, devp);
  545. if (ret) {
  546. debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
  547. return ret;
  548. }
  549. debug("%s: No match found: bound generic driver instead\n", __func__);
  550. return 0;
  551. error:
  552. debug("%s: No match found: error %d\n", __func__, ret);
  553. return ret;
  554. }
  555. int pci_bind_bus_devices(struct udevice *bus)
  556. {
  557. ulong vendor, device;
  558. ulong header_type;
  559. pci_dev_t bdf, end;
  560. bool found_multi;
  561. int ret;
  562. found_multi = false;
  563. end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
  564. PCI_MAX_PCI_FUNCTIONS - 1);
  565. for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
  566. bdf += PCI_BDF(0, 0, 1)) {
  567. struct pci_child_platdata *pplat;
  568. struct udevice *dev;
  569. ulong class;
  570. if (PCI_FUNC(bdf) && !found_multi)
  571. continue;
  572. /* Check only the first access, we don't expect problems */
  573. ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
  574. &header_type, PCI_SIZE_8);
  575. if (ret)
  576. goto error;
  577. pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
  578. PCI_SIZE_16);
  579. if (vendor == 0xffff || vendor == 0x0000)
  580. continue;
  581. if (!PCI_FUNC(bdf))
  582. found_multi = header_type & 0x80;
  583. debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
  584. bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
  585. pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
  586. PCI_SIZE_16);
  587. pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
  588. PCI_SIZE_32);
  589. class >>= 8;
  590. /* Find this device in the device tree */
  591. ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
  592. /* If nothing in the device tree, bind a device */
  593. if (ret == -ENODEV) {
  594. struct pci_device_id find_id;
  595. ulong val;
  596. memset(&find_id, '\0', sizeof(find_id));
  597. find_id.vendor = vendor;
  598. find_id.device = device;
  599. find_id.class = class;
  600. if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
  601. pci_bus_read_config(bus, bdf,
  602. PCI_SUBSYSTEM_VENDOR_ID,
  603. &val, PCI_SIZE_32);
  604. find_id.subvendor = val & 0xffff;
  605. find_id.subdevice = val >> 16;
  606. }
  607. ret = pci_find_and_bind_driver(bus, &find_id, bdf,
  608. &dev);
  609. }
  610. if (ret == -EPERM)
  611. continue;
  612. else if (ret)
  613. return ret;
  614. /* Update the platform data */
  615. pplat = dev_get_parent_platdata(dev);
  616. pplat->devfn = PCI_MASK_BUS(bdf);
  617. pplat->vendor = vendor;
  618. pplat->device = device;
  619. pplat->class = class;
  620. }
  621. return 0;
  622. error:
  623. printf("Cannot read bus configuration: %d\n", ret);
  624. return ret;
  625. }
  626. static int decode_regions(struct pci_controller *hose, const void *blob,
  627. int parent_node, int node)
  628. {
  629. int pci_addr_cells, addr_cells, size_cells;
  630. phys_addr_t base = 0, size;
  631. int cells_per_record;
  632. const u32 *prop;
  633. int len;
  634. int i;
  635. prop = fdt_getprop(blob, node, "ranges", &len);
  636. if (!prop)
  637. return -EINVAL;
  638. pci_addr_cells = fdt_address_cells(blob, node);
  639. addr_cells = fdt_address_cells(blob, parent_node);
  640. size_cells = fdt_size_cells(blob, node);
  641. /* PCI addresses are always 3-cells */
  642. len /= sizeof(u32);
  643. cells_per_record = pci_addr_cells + addr_cells + size_cells;
  644. hose->region_count = 0;
  645. debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
  646. cells_per_record);
  647. for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
  648. u64 pci_addr, addr, size;
  649. int space_code;
  650. u32 flags;
  651. int type;
  652. int pos;
  653. if (len < cells_per_record)
  654. break;
  655. flags = fdt32_to_cpu(prop[0]);
  656. space_code = (flags >> 24) & 3;
  657. pci_addr = fdtdec_get_number(prop + 1, 2);
  658. prop += pci_addr_cells;
  659. addr = fdtdec_get_number(prop, addr_cells);
  660. prop += addr_cells;
  661. size = fdtdec_get_number(prop, size_cells);
  662. prop += size_cells;
  663. debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
  664. ", size=%" PRIx64 ", space_code=%d\n", __func__,
  665. hose->region_count, pci_addr, addr, size, space_code);
  666. if (space_code & 2) {
  667. type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
  668. PCI_REGION_MEM;
  669. } else if (space_code & 1) {
  670. type = PCI_REGION_IO;
  671. } else {
  672. continue;
  673. }
  674. pos = -1;
  675. for (i = 0; i < hose->region_count; i++) {
  676. if (hose->regions[i].flags == type)
  677. pos = i;
  678. }
  679. if (pos == -1)
  680. pos = hose->region_count++;
  681. debug(" - type=%d, pos=%d\n", type, pos);
  682. pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
  683. }
  684. /* Add a region for our local memory */
  685. size = gd->ram_size;
  686. #ifdef CONFIG_SYS_SDRAM_BASE
  687. base = CONFIG_SYS_SDRAM_BASE;
  688. #endif
  689. if (gd->pci_ram_top && gd->pci_ram_top < base + size)
  690. size = gd->pci_ram_top - base;
  691. pci_set_region(hose->regions + hose->region_count++, base, base,
  692. size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  693. return 0;
  694. }
  695. static int pci_uclass_pre_probe(struct udevice *bus)
  696. {
  697. struct pci_controller *hose;
  698. int ret;
  699. debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
  700. bus->parent->name);
  701. hose = bus->uclass_priv;
  702. /* For bridges, use the top-level PCI controller */
  703. if (!device_is_on_pci_bus(bus)) {
  704. hose->ctlr = bus;
  705. ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
  706. bus->of_offset);
  707. if (ret) {
  708. debug("%s: Cannot decode regions\n", __func__);
  709. return ret;
  710. }
  711. } else {
  712. struct pci_controller *parent_hose;
  713. parent_hose = dev_get_uclass_priv(bus->parent);
  714. hose->ctlr = parent_hose->bus;
  715. }
  716. hose->bus = bus;
  717. hose->first_busno = bus->seq;
  718. hose->last_busno = bus->seq;
  719. return 0;
  720. }
  721. static int pci_uclass_post_probe(struct udevice *bus)
  722. {
  723. int ret;
  724. debug("%s: probing bus %d\n", __func__, bus->seq);
  725. ret = pci_bind_bus_devices(bus);
  726. if (ret)
  727. return ret;
  728. #ifdef CONFIG_PCI_PNP
  729. ret = pci_auto_config_devices(bus);
  730. if (ret < 0)
  731. return ret;
  732. #endif
  733. #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
  734. /*
  735. * Per Intel FSP specification, we should call FSP notify API to
  736. * inform FSP that PCI enumeration has been done so that FSP will
  737. * do any necessary initialization as required by the chipset's
  738. * BIOS Writer's Guide (BWG).
  739. *
  740. * Unfortunately we have to put this call here as with driver model,
  741. * the enumeration is all done on a lazy basis as needed, so until
  742. * something is touched on PCI it won't happen.
  743. *
  744. * Note we only call this 1) after U-Boot is relocated, and 2)
  745. * root bus has finished probing.
  746. */
  747. if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
  748. ret = fsp_init_phase_pci();
  749. if (ret)
  750. return ret;
  751. }
  752. #endif
  753. return 0;
  754. }
  755. static int pci_uclass_child_post_bind(struct udevice *dev)
  756. {
  757. struct pci_child_platdata *pplat;
  758. struct fdt_pci_addr addr;
  759. int ret;
  760. if (dev->of_offset == -1)
  761. return 0;
  762. /*
  763. * We could read vendor, device, class if available. But for now we
  764. * just check the address.
  765. */
  766. pplat = dev_get_parent_platdata(dev);
  767. ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
  768. FDT_PCI_SPACE_CONFIG, "reg", &addr);
  769. if (ret) {
  770. if (ret != -ENOENT)
  771. return -EINVAL;
  772. } else {
  773. /* extract the devfn from fdt_pci_addr */
  774. pplat->devfn = addr.phys_hi & 0xff00;
  775. }
  776. return 0;
  777. }
  778. static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
  779. uint offset, ulong *valuep,
  780. enum pci_size_t size)
  781. {
  782. struct pci_controller *hose = bus->uclass_priv;
  783. return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
  784. }
  785. static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
  786. uint offset, ulong value,
  787. enum pci_size_t size)
  788. {
  789. struct pci_controller *hose = bus->uclass_priv;
  790. return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
  791. }
  792. static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
  793. {
  794. struct udevice *dev;
  795. int ret = 0;
  796. /*
  797. * Scan through all the PCI controllers. On x86 there will only be one
  798. * but that is not necessarily true on other hardware.
  799. */
  800. do {
  801. device_find_first_child(bus, &dev);
  802. if (dev) {
  803. *devp = dev;
  804. return 0;
  805. }
  806. ret = uclass_next_device(&bus);
  807. if (ret)
  808. return ret;
  809. } while (bus);
  810. return 0;
  811. }
  812. int pci_find_next_device(struct udevice **devp)
  813. {
  814. struct udevice *child = *devp;
  815. struct udevice *bus = child->parent;
  816. int ret;
  817. /* First try all the siblings */
  818. *devp = NULL;
  819. while (child) {
  820. device_find_next_child(&child);
  821. if (child) {
  822. *devp = child;
  823. return 0;
  824. }
  825. }
  826. /* We ran out of siblings. Try the next bus */
  827. ret = uclass_next_device(&bus);
  828. if (ret)
  829. return ret;
  830. return bus ? skip_to_next_device(bus, devp) : 0;
  831. }
  832. int pci_find_first_device(struct udevice **devp)
  833. {
  834. struct udevice *bus;
  835. int ret;
  836. *devp = NULL;
  837. ret = uclass_first_device(UCLASS_PCI, &bus);
  838. if (ret)
  839. return ret;
  840. return skip_to_next_device(bus, devp);
  841. }
  842. ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
  843. {
  844. switch (size) {
  845. case PCI_SIZE_8:
  846. return (value >> ((offset & 3) * 8)) & 0xff;
  847. case PCI_SIZE_16:
  848. return (value >> ((offset & 2) * 8)) & 0xffff;
  849. default:
  850. return value;
  851. }
  852. }
  853. ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
  854. enum pci_size_t size)
  855. {
  856. uint off_mask;
  857. uint val_mask, shift;
  858. ulong ldata, mask;
  859. switch (size) {
  860. case PCI_SIZE_8:
  861. off_mask = 3;
  862. val_mask = 0xff;
  863. break;
  864. case PCI_SIZE_16:
  865. off_mask = 2;
  866. val_mask = 0xffff;
  867. break;
  868. default:
  869. return value;
  870. }
  871. shift = (offset & off_mask) * 8;
  872. ldata = (value & val_mask) << shift;
  873. mask = val_mask << shift;
  874. value = (old & ~mask) | ldata;
  875. return value;
  876. }
  877. int pci_get_regions(struct udevice *dev, struct pci_region **iop,
  878. struct pci_region **memp, struct pci_region **prefp)
  879. {
  880. struct udevice *bus = pci_get_controller(dev);
  881. struct pci_controller *hose = dev_get_uclass_priv(bus);
  882. int i;
  883. *iop = NULL;
  884. *memp = NULL;
  885. *prefp = NULL;
  886. for (i = 0; i < hose->region_count; i++) {
  887. switch (hose->regions[i].flags) {
  888. case PCI_REGION_IO:
  889. if (!*iop || (*iop)->size < hose->regions[i].size)
  890. *iop = hose->regions + i;
  891. break;
  892. case PCI_REGION_MEM:
  893. if (!*memp || (*memp)->size < hose->regions[i].size)
  894. *memp = hose->regions + i;
  895. break;
  896. case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
  897. if (!*prefp || (*prefp)->size < hose->regions[i].size)
  898. *prefp = hose->regions + i;
  899. break;
  900. }
  901. }
  902. return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
  903. }
  904. u32 dm_pci_read_bar32(struct udevice *dev, int barnum)
  905. {
  906. u32 addr;
  907. int bar;
  908. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  909. dm_pci_read_config32(dev, bar, &addr);
  910. if (addr & PCI_BASE_ADDRESS_SPACE_IO)
  911. return addr & PCI_BASE_ADDRESS_IO_MASK;
  912. else
  913. return addr & PCI_BASE_ADDRESS_MEM_MASK;
  914. }
  915. void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
  916. {
  917. int bar;
  918. bar = PCI_BASE_ADDRESS_0 + barnum * 4;
  919. dm_pci_write_config32(dev, bar, addr);
  920. }
  921. static int _dm_pci_bus_to_phys(struct udevice *ctlr,
  922. pci_addr_t bus_addr, unsigned long flags,
  923. unsigned long skip_mask, phys_addr_t *pa)
  924. {
  925. struct pci_controller *hose = dev_get_uclass_priv(ctlr);
  926. struct pci_region *res;
  927. int i;
  928. for (i = 0; i < hose->region_count; i++) {
  929. res = &hose->regions[i];
  930. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  931. continue;
  932. if (res->flags & skip_mask)
  933. continue;
  934. if (bus_addr >= res->bus_start &&
  935. (bus_addr - res->bus_start) < res->size) {
  936. *pa = (bus_addr - res->bus_start + res->phys_start);
  937. return 0;
  938. }
  939. }
  940. return 1;
  941. }
  942. phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
  943. unsigned long flags)
  944. {
  945. phys_addr_t phys_addr = 0;
  946. struct udevice *ctlr;
  947. int ret;
  948. /* The root controller has the region information */
  949. ctlr = pci_get_controller(dev);
  950. /*
  951. * if PCI_REGION_MEM is set we do a two pass search with preference
  952. * on matches that don't have PCI_REGION_SYS_MEMORY set
  953. */
  954. if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
  955. ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
  956. flags, PCI_REGION_SYS_MEMORY,
  957. &phys_addr);
  958. if (!ret)
  959. return phys_addr;
  960. }
  961. ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
  962. if (ret)
  963. puts("pci_hose_bus_to_phys: invalid physical address\n");
  964. return phys_addr;
  965. }
  966. int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
  967. unsigned long flags, unsigned long skip_mask,
  968. pci_addr_t *ba)
  969. {
  970. struct pci_region *res;
  971. struct udevice *ctlr;
  972. pci_addr_t bus_addr;
  973. int i;
  974. struct pci_controller *hose;
  975. /* The root controller has the region information */
  976. ctlr = pci_get_controller(dev);
  977. hose = dev_get_uclass_priv(ctlr);
  978. for (i = 0; i < hose->region_count; i++) {
  979. res = &hose->regions[i];
  980. if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
  981. continue;
  982. if (res->flags & skip_mask)
  983. continue;
  984. bus_addr = phys_addr - res->phys_start + res->bus_start;
  985. if (bus_addr >= res->bus_start &&
  986. (bus_addr - res->bus_start) < res->size) {
  987. *ba = bus_addr;
  988. return 0;
  989. }
  990. }
  991. return 1;
  992. }
  993. pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
  994. unsigned long flags)
  995. {
  996. pci_addr_t bus_addr = 0;
  997. int ret;
  998. /*
  999. * if PCI_REGION_MEM is set we do a two pass search with preference
  1000. * on matches that don't have PCI_REGION_SYS_MEMORY set
  1001. */
  1002. if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
  1003. ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
  1004. PCI_REGION_SYS_MEMORY, &bus_addr);
  1005. if (!ret)
  1006. return bus_addr;
  1007. }
  1008. ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
  1009. if (ret)
  1010. puts("pci_hose_phys_to_bus: invalid physical address\n");
  1011. return bus_addr;
  1012. }
  1013. void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
  1014. {
  1015. pci_addr_t pci_bus_addr;
  1016. u32 bar_response;
  1017. /* read BAR address */
  1018. dm_pci_read_config32(dev, bar, &bar_response);
  1019. pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
  1020. /*
  1021. * Pass "0" as the length argument to pci_bus_to_virt. The arg
  1022. * isn't actualy used on any platform because u-boot assumes a static
  1023. * linear mapping. In the future, this could read the BAR size
  1024. * and pass that as the size if needed.
  1025. */
  1026. return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
  1027. }
  1028. UCLASS_DRIVER(pci) = {
  1029. .id = UCLASS_PCI,
  1030. .name = "pci",
  1031. .flags = DM_UC_FLAG_SEQ_ALIAS,
  1032. .post_bind = dm_scan_fdt_dev,
  1033. .pre_probe = pci_uclass_pre_probe,
  1034. .post_probe = pci_uclass_post_probe,
  1035. .child_post_bind = pci_uclass_child_post_bind,
  1036. .per_device_auto_alloc_size = sizeof(struct pci_controller),
  1037. .per_child_platdata_auto_alloc_size =
  1038. sizeof(struct pci_child_platdata),
  1039. };
  1040. static const struct dm_pci_ops pci_bridge_ops = {
  1041. .read_config = pci_bridge_read_config,
  1042. .write_config = pci_bridge_write_config,
  1043. };
  1044. static const struct udevice_id pci_bridge_ids[] = {
  1045. { .compatible = "pci-bridge" },
  1046. { }
  1047. };
  1048. U_BOOT_DRIVER(pci_bridge_drv) = {
  1049. .name = "pci_bridge_drv",
  1050. .id = UCLASS_PCI,
  1051. .of_match = pci_bridge_ids,
  1052. .ops = &pci_bridge_ops,
  1053. };
  1054. UCLASS_DRIVER(pci_generic) = {
  1055. .id = UCLASS_PCI_GENERIC,
  1056. .name = "pci_generic",
  1057. };
  1058. static const struct udevice_id pci_generic_ids[] = {
  1059. { .compatible = "pci-generic" },
  1060. { }
  1061. };
  1062. U_BOOT_DRIVER(pci_generic_drv) = {
  1063. .name = "pci_generic_drv",
  1064. .id = UCLASS_PCI_GENERIC,
  1065. .of_match = pci_generic_ids,
  1066. };
  1067. void pci_init(void)
  1068. {
  1069. struct udevice *bus;
  1070. /*
  1071. * Enumerate all known controller devices. Enumeration has the side-
  1072. * effect of probing them, so PCIe devices will be enumerated too.
  1073. */
  1074. for (uclass_first_device(UCLASS_PCI, &bus);
  1075. bus;
  1076. uclass_next_device(&bus)) {
  1077. ;
  1078. }
  1079. }