pci-uclass.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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 <dm/lists.h>
  14. #include <dm/root.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. DECLARE_GLOBAL_DATA_PTR;
  20. static int pci_get_bus(int busnum, struct udevice **busp)
  21. {
  22. int ret;
  23. ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
  24. /* Since buses may not be numbered yet try a little harder with bus 0 */
  25. if (ret == -ENODEV) {
  26. ret = uclass_first_device(UCLASS_PCI, busp);
  27. if (ret)
  28. return ret;
  29. else if (!*busp)
  30. return -ENODEV;
  31. ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
  32. }
  33. return ret;
  34. }
  35. struct pci_controller *pci_bus_to_hose(int busnum)
  36. {
  37. struct udevice *bus;
  38. int ret;
  39. ret = pci_get_bus(busnum, &bus);
  40. if (ret) {
  41. debug("%s: Cannot get bus %d: ret=%d\n", __func__, busnum, ret);
  42. return NULL;
  43. }
  44. return dev_get_uclass_priv(bus);
  45. }
  46. pci_dev_t pci_get_bdf(struct udevice *dev)
  47. {
  48. struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
  49. struct udevice *bus = dev->parent;
  50. return PCI_ADD_BUS(bus->seq, pplat->devfn);
  51. }
  52. /**
  53. * pci_get_bus_max() - returns the bus number of the last active bus
  54. *
  55. * @return last bus number, or -1 if no active buses
  56. */
  57. static int pci_get_bus_max(void)
  58. {
  59. struct udevice *bus;
  60. struct uclass *uc;
  61. int ret = -1;
  62. ret = uclass_get(UCLASS_PCI, &uc);
  63. uclass_foreach_dev(bus, uc) {
  64. if (bus->seq > ret)
  65. ret = bus->seq;
  66. }
  67. debug("%s: ret=%d\n", __func__, ret);
  68. return ret;
  69. }
  70. int pci_last_busno(void)
  71. {
  72. return pci_get_bus_max();
  73. }
  74. int pci_get_ff(enum pci_size_t size)
  75. {
  76. switch (size) {
  77. case PCI_SIZE_8:
  78. return 0xff;
  79. case PCI_SIZE_16:
  80. return 0xffff;
  81. default:
  82. return 0xffffffff;
  83. }
  84. }
  85. int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
  86. struct udevice **devp)
  87. {
  88. struct udevice *dev;
  89. for (device_find_first_child(bus, &dev);
  90. dev;
  91. device_find_next_child(&dev)) {
  92. struct pci_child_platdata *pplat;
  93. pplat = dev_get_parent_platdata(dev);
  94. if (pplat && pplat->devfn == find_devfn) {
  95. *devp = dev;
  96. return 0;
  97. }
  98. }
  99. return -ENODEV;
  100. }
  101. int pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
  102. {
  103. struct udevice *bus;
  104. int ret;
  105. ret = pci_get_bus(PCI_BUS(bdf), &bus);
  106. if (ret)
  107. return ret;
  108. return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
  109. }
  110. static int pci_device_matches_ids(struct udevice *dev,
  111. struct pci_device_id *ids)
  112. {
  113. struct pci_child_platdata *pplat;
  114. int i;
  115. pplat = dev_get_parent_platdata(dev);
  116. if (!pplat)
  117. return -EINVAL;
  118. for (i = 0; ids[i].vendor != 0; i++) {
  119. if (pplat->vendor == ids[i].vendor &&
  120. pplat->device == ids[i].device)
  121. return i;
  122. }
  123. return -EINVAL;
  124. }
  125. int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
  126. int *indexp, struct udevice **devp)
  127. {
  128. struct udevice *dev;
  129. /* Scan all devices on this bus */
  130. for (device_find_first_child(bus, &dev);
  131. dev;
  132. device_find_next_child(&dev)) {
  133. if (pci_device_matches_ids(dev, ids) >= 0) {
  134. if ((*indexp)-- <= 0) {
  135. *devp = dev;
  136. return 0;
  137. }
  138. }
  139. }
  140. return -ENODEV;
  141. }
  142. int pci_find_device_id(struct pci_device_id *ids, int index,
  143. struct udevice **devp)
  144. {
  145. struct udevice *bus;
  146. /* Scan all known buses */
  147. for (uclass_first_device(UCLASS_PCI, &bus);
  148. bus;
  149. uclass_next_device(&bus)) {
  150. if (!pci_bus_find_devices(bus, ids, &index, devp))
  151. return 0;
  152. }
  153. *devp = NULL;
  154. return -ENODEV;
  155. }
  156. int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
  157. unsigned long value, enum pci_size_t size)
  158. {
  159. struct dm_pci_ops *ops;
  160. ops = pci_get_ops(bus);
  161. if (!ops->write_config)
  162. return -ENOSYS;
  163. return ops->write_config(bus, bdf, offset, value, size);
  164. }
  165. int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
  166. enum pci_size_t size)
  167. {
  168. struct udevice *bus;
  169. int ret;
  170. ret = pci_get_bus(PCI_BUS(bdf), &bus);
  171. if (ret)
  172. return ret;
  173. return pci_bus_write_config(bus, bdf, offset, value, size);
  174. }
  175. int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
  176. enum pci_size_t size)
  177. {
  178. struct udevice *bus;
  179. for (bus = dev; device_is_on_pci_bus(bus);)
  180. bus = bus->parent;
  181. return pci_bus_write_config(bus, pci_get_bdf(dev), offset, value, size);
  182. }
  183. int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
  184. {
  185. return pci_write_config(bdf, offset, value, PCI_SIZE_32);
  186. }
  187. int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
  188. {
  189. return pci_write_config(bdf, offset, value, PCI_SIZE_16);
  190. }
  191. int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
  192. {
  193. return pci_write_config(bdf, offset, value, PCI_SIZE_8);
  194. }
  195. int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
  196. {
  197. return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
  198. }
  199. int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
  200. {
  201. return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
  202. }
  203. int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
  204. {
  205. return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
  206. }
  207. int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
  208. unsigned long *valuep, enum pci_size_t size)
  209. {
  210. struct dm_pci_ops *ops;
  211. ops = pci_get_ops(bus);
  212. if (!ops->read_config)
  213. return -ENOSYS;
  214. return ops->read_config(bus, bdf, offset, valuep, size);
  215. }
  216. int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
  217. enum pci_size_t size)
  218. {
  219. struct udevice *bus;
  220. int ret;
  221. ret = pci_get_bus(PCI_BUS(bdf), &bus);
  222. if (ret)
  223. return ret;
  224. return pci_bus_read_config(bus, bdf, offset, valuep, size);
  225. }
  226. int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
  227. enum pci_size_t size)
  228. {
  229. struct udevice *bus;
  230. for (bus = dev; device_is_on_pci_bus(bus);)
  231. bus = bus->parent;
  232. return pci_bus_read_config(bus, pci_get_bdf(dev), offset, valuep,
  233. size);
  234. }
  235. int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
  236. {
  237. unsigned long value;
  238. int ret;
  239. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
  240. if (ret)
  241. return ret;
  242. *valuep = value;
  243. return 0;
  244. }
  245. int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
  246. {
  247. unsigned long value;
  248. int ret;
  249. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
  250. if (ret)
  251. return ret;
  252. *valuep = value;
  253. return 0;
  254. }
  255. int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
  256. {
  257. unsigned long value;
  258. int ret;
  259. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
  260. if (ret)
  261. return ret;
  262. *valuep = value;
  263. return 0;
  264. }
  265. int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
  266. {
  267. unsigned long value;
  268. int ret;
  269. ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
  270. if (ret)
  271. return ret;
  272. *valuep = value;
  273. return 0;
  274. }
  275. int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
  276. {
  277. unsigned long value;
  278. int ret;
  279. ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
  280. if (ret)
  281. return ret;
  282. *valuep = value;
  283. return 0;
  284. }
  285. int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
  286. {
  287. unsigned long value;
  288. int ret;
  289. ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
  290. if (ret)
  291. return ret;
  292. *valuep = value;
  293. return 0;
  294. }
  295. static void set_vga_bridge_bits(struct udevice *dev)
  296. {
  297. struct udevice *parent = dev->parent;
  298. u16 bc;
  299. while (parent->seq != 0) {
  300. dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
  301. bc |= PCI_BRIDGE_CTL_VGA;
  302. dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
  303. parent = parent->parent;
  304. }
  305. }
  306. int pci_auto_config_devices(struct udevice *bus)
  307. {
  308. struct pci_controller *hose = bus->uclass_priv;
  309. struct pci_child_platdata *pplat;
  310. unsigned int sub_bus;
  311. struct udevice *dev;
  312. int ret;
  313. sub_bus = bus->seq;
  314. debug("%s: start\n", __func__);
  315. pciauto_config_init(hose);
  316. for (ret = device_find_first_child(bus, &dev);
  317. !ret && dev;
  318. ret = device_find_next_child(&dev)) {
  319. unsigned int max_bus;
  320. int ret;
  321. debug("%s: device %s\n", __func__, dev->name);
  322. ret = pciauto_config_device(hose, pci_get_bdf(dev));
  323. if (ret < 0)
  324. return ret;
  325. max_bus = ret;
  326. sub_bus = max(sub_bus, max_bus);
  327. pplat = dev_get_parent_platdata(dev);
  328. if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
  329. set_vga_bridge_bits(dev);
  330. }
  331. debug("%s: done\n", __func__);
  332. return sub_bus;
  333. }
  334. int dm_pci_hose_probe_bus(struct pci_controller *hose, pci_dev_t bdf)
  335. {
  336. struct udevice *parent, *bus;
  337. int sub_bus;
  338. int ret;
  339. debug("%s\n", __func__);
  340. parent = hose->bus;
  341. /* Find the bus within the parent */
  342. ret = pci_bus_find_devfn(parent, PCI_MASK_BUS(bdf), &bus);
  343. if (ret) {
  344. debug("%s: Cannot find device %x on bus %s: %d\n", __func__,
  345. bdf, parent->name, ret);
  346. return ret;
  347. }
  348. sub_bus = pci_get_bus_max() + 1;
  349. debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
  350. pciauto_prescan_setup_bridge(hose, bdf, sub_bus);
  351. ret = device_probe(bus);
  352. if (ret) {
  353. debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
  354. ret);
  355. return ret;
  356. }
  357. if (sub_bus != bus->seq) {
  358. printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
  359. __func__, bus->name, bus->seq, sub_bus);
  360. return -EPIPE;
  361. }
  362. sub_bus = pci_get_bus_max();
  363. pciauto_postscan_setup_bridge(hose, bdf, sub_bus);
  364. return sub_bus;
  365. }
  366. /**
  367. * pci_match_one_device - Tell if a PCI device structure has a matching
  368. * PCI device id structure
  369. * @id: single PCI device id structure to match
  370. * @dev: the PCI device structure to match against
  371. *
  372. * Returns the matching pci_device_id structure or %NULL if there is no match.
  373. */
  374. static bool pci_match_one_id(const struct pci_device_id *id,
  375. const struct pci_device_id *find)
  376. {
  377. if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
  378. (id->device == PCI_ANY_ID || id->device == find->device) &&
  379. (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
  380. (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
  381. !((id->class ^ find->class) & id->class_mask))
  382. return true;
  383. return false;
  384. }
  385. /**
  386. * pci_find_and_bind_driver() - Find and bind the right PCI driver
  387. *
  388. * This only looks at certain fields in the descriptor.
  389. *
  390. * @parent: Parent bus
  391. * @find_id: Specification of the driver to find
  392. * @bdf: Bus/device/function addreess - see PCI_BDF()
  393. * @devp: Returns a pointer to the device created
  394. * @return 0 if OK, -EPERM if the device is not needed before relocation and
  395. * therefore was not created, other -ve value on error
  396. */
  397. static int pci_find_and_bind_driver(struct udevice *parent,
  398. struct pci_device_id *find_id,
  399. pci_dev_t bdf, struct udevice **devp)
  400. {
  401. struct pci_driver_entry *start, *entry;
  402. const char *drv;
  403. int n_ents;
  404. int ret;
  405. char name[30], *str;
  406. bool bridge;
  407. *devp = NULL;
  408. debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
  409. find_id->vendor, find_id->device);
  410. start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
  411. n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
  412. for (entry = start; entry != start + n_ents; entry++) {
  413. const struct pci_device_id *id;
  414. struct udevice *dev;
  415. const struct driver *drv;
  416. for (id = entry->match;
  417. id->vendor || id->subvendor || id->class_mask;
  418. id++) {
  419. if (!pci_match_one_id(id, find_id))
  420. continue;
  421. drv = entry->driver;
  422. /*
  423. * In the pre-relocation phase, we only bind devices
  424. * whose driver has the DM_FLAG_PRE_RELOC set, to save
  425. * precious memory space as on some platforms as that
  426. * space is pretty limited (ie: using Cache As RAM).
  427. */
  428. if (!(gd->flags & GD_FLG_RELOC) &&
  429. !(drv->flags & DM_FLAG_PRE_RELOC))
  430. return -EPERM;
  431. /*
  432. * We could pass the descriptor to the driver as
  433. * platdata (instead of NULL) and allow its bind()
  434. * method to return -ENOENT if it doesn't support this
  435. * device. That way we could continue the search to
  436. * find another driver. For now this doesn't seem
  437. * necesssary, so just bind the first match.
  438. */
  439. ret = device_bind(parent, drv, drv->name, NULL, -1,
  440. &dev);
  441. if (ret)
  442. goto error;
  443. debug("%s: Match found: %s\n", __func__, drv->name);
  444. dev->driver_data = find_id->driver_data;
  445. *devp = dev;
  446. return 0;
  447. }
  448. }
  449. bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
  450. /*
  451. * In the pre-relocation phase, we only bind bridge devices to save
  452. * precious memory space as on some platforms as that space is pretty
  453. * limited (ie: using Cache As RAM).
  454. */
  455. if (!(gd->flags & GD_FLG_RELOC) && !bridge)
  456. return -EPERM;
  457. /* Bind a generic driver so that the device can be used */
  458. sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
  459. PCI_FUNC(bdf));
  460. str = strdup(name);
  461. if (!str)
  462. return -ENOMEM;
  463. drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
  464. ret = device_bind_driver(parent, drv, str, devp);
  465. if (ret) {
  466. debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
  467. return ret;
  468. }
  469. debug("%s: No match found: bound generic driver instead\n", __func__);
  470. return 0;
  471. error:
  472. debug("%s: No match found: error %d\n", __func__, ret);
  473. return ret;
  474. }
  475. int pci_bind_bus_devices(struct udevice *bus)
  476. {
  477. ulong vendor, device;
  478. ulong header_type;
  479. pci_dev_t bdf, end;
  480. bool found_multi;
  481. int ret;
  482. found_multi = false;
  483. end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
  484. PCI_MAX_PCI_FUNCTIONS - 1);
  485. for (bdf = PCI_BDF(bus->seq, 0, 0); bdf < end;
  486. bdf += PCI_BDF(0, 0, 1)) {
  487. struct pci_child_platdata *pplat;
  488. struct udevice *dev;
  489. ulong class;
  490. if (PCI_FUNC(bdf) && !found_multi)
  491. continue;
  492. /* Check only the first access, we don't expect problems */
  493. ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
  494. &header_type, PCI_SIZE_8);
  495. if (ret)
  496. goto error;
  497. pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
  498. PCI_SIZE_16);
  499. if (vendor == 0xffff || vendor == 0x0000)
  500. continue;
  501. if (!PCI_FUNC(bdf))
  502. found_multi = header_type & 0x80;
  503. debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
  504. bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
  505. pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
  506. PCI_SIZE_16);
  507. pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
  508. PCI_SIZE_32);
  509. class >>= 8;
  510. /* Find this device in the device tree */
  511. ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
  512. /* Search for a driver */
  513. /* If nothing in the device tree, bind a generic device */
  514. if (ret == -ENODEV) {
  515. struct pci_device_id find_id;
  516. ulong val;
  517. memset(&find_id, '\0', sizeof(find_id));
  518. find_id.vendor = vendor;
  519. find_id.device = device;
  520. find_id.class = class;
  521. if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
  522. pci_bus_read_config(bus, bdf,
  523. PCI_SUBSYSTEM_VENDOR_ID,
  524. &val, PCI_SIZE_32);
  525. find_id.subvendor = val & 0xffff;
  526. find_id.subdevice = val >> 16;
  527. }
  528. ret = pci_find_and_bind_driver(bus, &find_id, bdf,
  529. &dev);
  530. }
  531. if (ret == -EPERM)
  532. continue;
  533. else if (ret)
  534. return ret;
  535. /* Update the platform data */
  536. pplat = dev_get_parent_platdata(dev);
  537. pplat->devfn = PCI_MASK_BUS(bdf);
  538. pplat->vendor = vendor;
  539. pplat->device = device;
  540. pplat->class = class;
  541. }
  542. return 0;
  543. error:
  544. printf("Cannot read bus configuration: %d\n", ret);
  545. return ret;
  546. }
  547. static int pci_uclass_post_bind(struct udevice *bus)
  548. {
  549. /*
  550. * If there is no pci device listed in the device tree,
  551. * don't bother scanning the device tree.
  552. */
  553. if (bus->of_offset == -1)
  554. return 0;
  555. /*
  556. * Scan the device tree for devices. This does not probe the PCI bus,
  557. * as this is not permitted while binding. It just finds devices
  558. * mentioned in the device tree.
  559. *
  560. * Before relocation, only bind devices marked for pre-relocation
  561. * use.
  562. */
  563. return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
  564. gd->flags & GD_FLG_RELOC ? false : true);
  565. }
  566. static int decode_regions(struct pci_controller *hose, const void *blob,
  567. int parent_node, int node)
  568. {
  569. int pci_addr_cells, addr_cells, size_cells;
  570. int cells_per_record;
  571. phys_addr_t addr;
  572. const u32 *prop;
  573. int len;
  574. int i;
  575. prop = fdt_getprop(blob, node, "ranges", &len);
  576. if (!prop)
  577. return -EINVAL;
  578. pci_addr_cells = fdt_address_cells(blob, node);
  579. addr_cells = fdt_address_cells(blob, parent_node);
  580. size_cells = fdt_size_cells(blob, node);
  581. /* PCI addresses are always 3-cells */
  582. len /= sizeof(u32);
  583. cells_per_record = pci_addr_cells + addr_cells + size_cells;
  584. hose->region_count = 0;
  585. debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
  586. cells_per_record);
  587. for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
  588. u64 pci_addr, addr, size;
  589. int space_code;
  590. u32 flags;
  591. int type;
  592. if (len < cells_per_record)
  593. break;
  594. flags = fdt32_to_cpu(prop[0]);
  595. space_code = (flags >> 24) & 3;
  596. pci_addr = fdtdec_get_number(prop + 1, 2);
  597. prop += pci_addr_cells;
  598. addr = fdtdec_get_number(prop, addr_cells);
  599. prop += addr_cells;
  600. size = fdtdec_get_number(prop, size_cells);
  601. prop += size_cells;
  602. debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
  603. ", size=%" PRIx64 ", space_code=%d\n", __func__,
  604. hose->region_count, pci_addr, addr, size, space_code);
  605. if (space_code & 2) {
  606. type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
  607. PCI_REGION_MEM;
  608. } else if (space_code & 1) {
  609. type = PCI_REGION_IO;
  610. } else {
  611. continue;
  612. }
  613. debug(" - type=%d\n", type);
  614. pci_set_region(hose->regions + hose->region_count++, pci_addr,
  615. addr, size, type);
  616. }
  617. /* Add a region for our local memory */
  618. addr = gd->ram_size;
  619. if (gd->pci_ram_top && gd->pci_ram_top < addr)
  620. addr = gd->pci_ram_top;
  621. pci_set_region(hose->regions + hose->region_count++, 0, 0, addr,
  622. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  623. return 0;
  624. }
  625. static int pci_uclass_pre_probe(struct udevice *bus)
  626. {
  627. struct pci_controller *hose;
  628. int ret;
  629. debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
  630. bus->parent->name);
  631. hose = bus->uclass_priv;
  632. /* For bridges, use the top-level PCI controller */
  633. if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
  634. hose->ctlr = bus;
  635. ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
  636. bus->of_offset);
  637. if (ret) {
  638. debug("%s: Cannot decode regions\n", __func__);
  639. return ret;
  640. }
  641. } else {
  642. struct pci_controller *parent_hose;
  643. parent_hose = dev_get_uclass_priv(bus->parent);
  644. hose->ctlr = parent_hose->bus;
  645. }
  646. hose->bus = bus;
  647. hose->first_busno = bus->seq;
  648. hose->last_busno = bus->seq;
  649. return 0;
  650. }
  651. static int pci_uclass_post_probe(struct udevice *bus)
  652. {
  653. int ret;
  654. debug("%s: probing bus %d\n", __func__, bus->seq);
  655. ret = pci_bind_bus_devices(bus);
  656. if (ret)
  657. return ret;
  658. #ifdef CONFIG_PCI_PNP
  659. ret = pci_auto_config_devices(bus);
  660. if (ret < 0)
  661. return ret;
  662. #endif
  663. #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
  664. /*
  665. * Per Intel FSP specification, we should call FSP notify API to
  666. * inform FSP that PCI enumeration has been done so that FSP will
  667. * do any necessary initialization as required by the chipset's
  668. * BIOS Writer's Guide (BWG).
  669. *
  670. * Unfortunately we have to put this call here as with driver model,
  671. * the enumeration is all done on a lazy basis as needed, so until
  672. * something is touched on PCI it won't happen.
  673. *
  674. * Note we only call this 1) after U-Boot is relocated, and 2)
  675. * root bus has finished probing.
  676. */
  677. if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
  678. ret = fsp_init_phase_pci();
  679. if (ret)
  680. return ret;
  681. }
  682. #endif
  683. return 0;
  684. }
  685. static int pci_uclass_child_post_bind(struct udevice *dev)
  686. {
  687. struct pci_child_platdata *pplat;
  688. struct fdt_pci_addr addr;
  689. int ret;
  690. if (dev->of_offset == -1)
  691. return 0;
  692. /*
  693. * We could read vendor, device, class if available. But for now we
  694. * just check the address.
  695. */
  696. pplat = dev_get_parent_platdata(dev);
  697. ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
  698. FDT_PCI_SPACE_CONFIG, "reg", &addr);
  699. if (ret) {
  700. if (ret != -ENOENT)
  701. return -EINVAL;
  702. } else {
  703. /* extract the devfn from fdt_pci_addr */
  704. pplat->devfn = addr.phys_hi & 0xff00;
  705. }
  706. return 0;
  707. }
  708. static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
  709. uint offset, ulong *valuep,
  710. enum pci_size_t size)
  711. {
  712. struct pci_controller *hose = bus->uclass_priv;
  713. return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
  714. }
  715. static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
  716. uint offset, ulong value,
  717. enum pci_size_t size)
  718. {
  719. struct pci_controller *hose = bus->uclass_priv;
  720. return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
  721. }
  722. static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
  723. {
  724. struct udevice *dev;
  725. int ret = 0;
  726. /*
  727. * Scan through all the PCI controllers. On x86 there will only be one
  728. * but that is not necessarily true on other hardware.
  729. */
  730. do {
  731. device_find_first_child(bus, &dev);
  732. if (dev) {
  733. *devp = dev;
  734. return 0;
  735. }
  736. ret = uclass_next_device(&bus);
  737. if (ret)
  738. return ret;
  739. } while (bus);
  740. return 0;
  741. }
  742. int pci_find_next_device(struct udevice **devp)
  743. {
  744. struct udevice *child = *devp;
  745. struct udevice *bus = child->parent;
  746. int ret;
  747. /* First try all the siblings */
  748. *devp = NULL;
  749. while (child) {
  750. device_find_next_child(&child);
  751. if (child) {
  752. *devp = child;
  753. return 0;
  754. }
  755. }
  756. /* We ran out of siblings. Try the next bus */
  757. ret = uclass_next_device(&bus);
  758. if (ret)
  759. return ret;
  760. return bus ? skip_to_next_device(bus, devp) : 0;
  761. }
  762. int pci_find_first_device(struct udevice **devp)
  763. {
  764. struct udevice *bus;
  765. int ret;
  766. *devp = NULL;
  767. ret = uclass_first_device(UCLASS_PCI, &bus);
  768. if (ret)
  769. return ret;
  770. return skip_to_next_device(bus, devp);
  771. }
  772. UCLASS_DRIVER(pci) = {
  773. .id = UCLASS_PCI,
  774. .name = "pci",
  775. .flags = DM_UC_FLAG_SEQ_ALIAS,
  776. .post_bind = pci_uclass_post_bind,
  777. .pre_probe = pci_uclass_pre_probe,
  778. .post_probe = pci_uclass_post_probe,
  779. .child_post_bind = pci_uclass_child_post_bind,
  780. .per_device_auto_alloc_size = sizeof(struct pci_controller),
  781. .per_child_platdata_auto_alloc_size =
  782. sizeof(struct pci_child_platdata),
  783. };
  784. static const struct dm_pci_ops pci_bridge_ops = {
  785. .read_config = pci_bridge_read_config,
  786. .write_config = pci_bridge_write_config,
  787. };
  788. static const struct udevice_id pci_bridge_ids[] = {
  789. { .compatible = "pci-bridge" },
  790. { }
  791. };
  792. U_BOOT_DRIVER(pci_bridge_drv) = {
  793. .name = "pci_bridge_drv",
  794. .id = UCLASS_PCI,
  795. .of_match = pci_bridge_ids,
  796. .ops = &pci_bridge_ops,
  797. };
  798. UCLASS_DRIVER(pci_generic) = {
  799. .id = UCLASS_PCI_GENERIC,
  800. .name = "pci_generic",
  801. };
  802. static const struct udevice_id pci_generic_ids[] = {
  803. { .compatible = "pci-generic" },
  804. { }
  805. };
  806. U_BOOT_DRIVER(pci_generic_drv) = {
  807. .name = "pci_generic_drv",
  808. .id = UCLASS_PCI_GENERIC,
  809. .of_match = pci_generic_ids,
  810. };