pci-uclass.c 29 KB

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