pci-uclass.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. DECLARE_GLOBAL_DATA_PTR;
  17. struct pci_controller *pci_bus_to_hose(int busnum)
  18. {
  19. struct udevice *bus;
  20. int ret;
  21. ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, &bus);
  22. if (ret) {
  23. debug("%s: Cannot get bus %d: ret=%d\n", __func__, busnum, ret);
  24. return NULL;
  25. }
  26. return dev_get_uclass_priv(bus);
  27. }
  28. pci_dev_t pci_get_bdf(struct udevice *dev)
  29. {
  30. struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
  31. struct udevice *bus = dev->parent;
  32. return PCI_ADD_BUS(bus->seq, pplat->devfn);
  33. }
  34. /**
  35. * pci_get_bus_max() - returns the bus number of the last active bus
  36. *
  37. * @return last bus number, or -1 if no active buses
  38. */
  39. static int pci_get_bus_max(void)
  40. {
  41. struct udevice *bus;
  42. struct uclass *uc;
  43. int ret = -1;
  44. ret = uclass_get(UCLASS_PCI, &uc);
  45. uclass_foreach_dev(bus, uc) {
  46. if (bus->seq > ret)
  47. ret = bus->seq;
  48. }
  49. debug("%s: ret=%d\n", __func__, ret);
  50. return ret;
  51. }
  52. int pci_last_busno(void)
  53. {
  54. struct pci_controller *hose;
  55. struct udevice *bus;
  56. struct uclass *uc;
  57. int ret;
  58. debug("pci_last_busno\n");
  59. ret = uclass_get(UCLASS_PCI, &uc);
  60. if (ret || list_empty(&uc->dev_head))
  61. return -1;
  62. /* Probe the last bus */
  63. bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
  64. debug("bus = %p, %s\n", bus, bus->name);
  65. assert(bus);
  66. ret = device_probe(bus);
  67. if (ret)
  68. return ret;
  69. /* If that bus has bridges, we may have new buses now. Get the last */
  70. bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
  71. hose = dev_get_uclass_priv(bus);
  72. debug("bus = %s, hose = %p\n", bus->name, hose);
  73. return hose->last_busno;
  74. }
  75. int pci_get_ff(enum pci_size_t size)
  76. {
  77. switch (size) {
  78. case PCI_SIZE_8:
  79. return 0xff;
  80. case PCI_SIZE_16:
  81. return 0xffff;
  82. default:
  83. return 0xffffffff;
  84. }
  85. }
  86. int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
  87. struct udevice **devp)
  88. {
  89. struct udevice *dev;
  90. for (device_find_first_child(bus, &dev);
  91. dev;
  92. device_find_next_child(&dev)) {
  93. struct pci_child_platdata *pplat;
  94. pplat = dev_get_parent_platdata(dev);
  95. if (pplat && pplat->devfn == find_devfn) {
  96. *devp = dev;
  97. return 0;
  98. }
  99. }
  100. return -ENODEV;
  101. }
  102. int pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
  103. {
  104. struct udevice *bus;
  105. int ret;
  106. ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
  107. if (ret)
  108. return ret;
  109. return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
  110. }
  111. static int pci_device_matches_ids(struct udevice *dev,
  112. struct pci_device_id *ids)
  113. {
  114. struct pci_child_platdata *pplat;
  115. int i;
  116. pplat = dev_get_parent_platdata(dev);
  117. if (!pplat)
  118. return -EINVAL;
  119. for (i = 0; ids[i].vendor != 0; i++) {
  120. if (pplat->vendor == ids[i].vendor &&
  121. pplat->device == ids[i].device)
  122. return i;
  123. }
  124. return -EINVAL;
  125. }
  126. int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
  127. int *indexp, struct udevice **devp)
  128. {
  129. struct udevice *dev;
  130. /* Scan all devices on this bus */
  131. for (device_find_first_child(bus, &dev);
  132. dev;
  133. device_find_next_child(&dev)) {
  134. if (pci_device_matches_ids(dev, ids) >= 0) {
  135. if ((*indexp)-- <= 0) {
  136. *devp = dev;
  137. return 0;
  138. }
  139. }
  140. }
  141. return -ENODEV;
  142. }
  143. int pci_find_device_id(struct pci_device_id *ids, int index,
  144. struct udevice **devp)
  145. {
  146. struct udevice *bus;
  147. /* Scan all known buses */
  148. for (uclass_first_device(UCLASS_PCI, &bus);
  149. bus;
  150. uclass_next_device(&bus)) {
  151. if (!pci_bus_find_devices(bus, ids, &index, devp))
  152. return 0;
  153. }
  154. *devp = NULL;
  155. return -ENODEV;
  156. }
  157. int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
  158. unsigned long value, enum pci_size_t size)
  159. {
  160. struct dm_pci_ops *ops;
  161. ops = pci_get_ops(bus);
  162. if (!ops->write_config)
  163. return -ENOSYS;
  164. return ops->write_config(bus, bdf, offset, value, size);
  165. }
  166. int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
  167. enum pci_size_t size)
  168. {
  169. struct udevice *bus;
  170. int ret;
  171. ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
  172. if (ret)
  173. return ret;
  174. return pci_bus_write_config(bus, bdf, offset, value, size);
  175. }
  176. int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
  177. {
  178. return pci_write_config(bdf, offset, value, PCI_SIZE_32);
  179. }
  180. int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
  181. {
  182. return pci_write_config(bdf, offset, value, PCI_SIZE_16);
  183. }
  184. int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
  185. {
  186. return pci_write_config(bdf, offset, value, PCI_SIZE_8);
  187. }
  188. int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
  189. unsigned long *valuep, enum pci_size_t size)
  190. {
  191. struct dm_pci_ops *ops;
  192. ops = pci_get_ops(bus);
  193. if (!ops->read_config)
  194. return -ENOSYS;
  195. return ops->read_config(bus, bdf, offset, valuep, size);
  196. }
  197. int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
  198. enum pci_size_t size)
  199. {
  200. struct udevice *bus;
  201. int ret;
  202. ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
  203. if (ret)
  204. return ret;
  205. return pci_bus_read_config(bus, bdf, offset, valuep, size);
  206. }
  207. int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
  208. {
  209. unsigned long value;
  210. int ret;
  211. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
  212. if (ret)
  213. return ret;
  214. *valuep = value;
  215. return 0;
  216. }
  217. int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
  218. {
  219. unsigned long value;
  220. int ret;
  221. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
  222. if (ret)
  223. return ret;
  224. *valuep = value;
  225. return 0;
  226. }
  227. int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
  228. {
  229. unsigned long value;
  230. int ret;
  231. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
  232. if (ret)
  233. return ret;
  234. *valuep = value;
  235. return 0;
  236. }
  237. int pci_auto_config_devices(struct udevice *bus)
  238. {
  239. struct pci_controller *hose = bus->uclass_priv;
  240. unsigned int sub_bus;
  241. struct udevice *dev;
  242. int ret;
  243. sub_bus = bus->seq;
  244. debug("%s: start\n", __func__);
  245. pciauto_config_init(hose);
  246. for (ret = device_find_first_child(bus, &dev);
  247. !ret && dev;
  248. ret = device_find_next_child(&dev)) {
  249. struct pci_controller *ctlr_hose;
  250. unsigned int max_bus;
  251. debug("%s: device %s\n", __func__, dev->name);
  252. /* The root controller has the region information */
  253. ctlr_hose = hose->ctlr->uclass_priv;
  254. max_bus = pciauto_config_device(ctlr_hose, pci_get_bdf(dev));
  255. sub_bus = max(sub_bus, max_bus);
  256. }
  257. debug("%s: done\n", __func__);
  258. return sub_bus;
  259. }
  260. int dm_pci_hose_probe_bus(struct pci_controller *hose, pci_dev_t bdf)
  261. {
  262. struct udevice *parent, *bus;
  263. int sub_bus;
  264. int ret;
  265. debug("%s\n", __func__);
  266. parent = hose->bus;
  267. /* Find the bus within the parent */
  268. ret = pci_bus_find_devfn(parent, PCI_MASK_BUS(bdf), &bus);
  269. if (ret) {
  270. debug("%s: Cannot find device %x on bus %s: %d\n", __func__,
  271. bdf, parent->name, ret);
  272. return ret;
  273. }
  274. sub_bus = pci_get_bus_max() + 1;
  275. debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
  276. pciauto_prescan_setup_bridge(hose, bdf, sub_bus);
  277. ret = device_probe(bus);
  278. if (ret) {
  279. debug("%s: Cannot probe bus bus %s: %d\n", __func__, bus->name,
  280. ret);
  281. return ret;
  282. }
  283. if (sub_bus != bus->seq) {
  284. printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
  285. __func__, bus->name, bus->seq, sub_bus);
  286. return -EPIPE;
  287. }
  288. sub_bus = pci_get_bus_max();
  289. pciauto_postscan_setup_bridge(hose, bdf, sub_bus);
  290. return sub_bus;
  291. }
  292. /**
  293. * pci_match_one_device - Tell if a PCI device structure has a matching
  294. * PCI device id structure
  295. * @id: single PCI device id structure to match
  296. * @dev: the PCI device structure to match against
  297. *
  298. * Returns the matching pci_device_id structure or %NULL if there is no match.
  299. */
  300. static bool pci_match_one_id(const struct pci_device_id *id,
  301. const struct pci_device_id *find)
  302. {
  303. if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
  304. (id->device == PCI_ANY_ID || id->device == find->device) &&
  305. (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
  306. (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
  307. !((id->class ^ find->class) & id->class_mask))
  308. return true;
  309. return false;
  310. }
  311. /**
  312. * pci_find_and_bind_driver() - Find and bind the right PCI driver
  313. *
  314. * This only looks at certain fields in the descriptor.
  315. */
  316. static int pci_find_and_bind_driver(struct udevice *parent,
  317. struct pci_device_id *find_id, pci_dev_t bdf,
  318. struct udevice **devp)
  319. {
  320. struct pci_driver_entry *start, *entry;
  321. const char *drv;
  322. int n_ents;
  323. int ret;
  324. char name[30], *str;
  325. *devp = NULL;
  326. debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
  327. find_id->vendor, find_id->device);
  328. start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
  329. n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
  330. for (entry = start; entry != start + n_ents; entry++) {
  331. const struct pci_device_id *id;
  332. struct udevice *dev;
  333. const struct driver *drv;
  334. for (id = entry->match;
  335. id->vendor || id->subvendor || id->class_mask;
  336. id++) {
  337. if (!pci_match_one_id(id, find_id))
  338. continue;
  339. drv = entry->driver;
  340. /*
  341. * We could pass the descriptor to the driver as
  342. * platdata (instead of NULL) and allow its bind()
  343. * method to return -ENOENT if it doesn't support this
  344. * device. That way we could continue the search to
  345. * find another driver. For now this doesn't seem
  346. * necesssary, so just bind the first match.
  347. */
  348. ret = device_bind(parent, drv, drv->name, NULL, -1,
  349. &dev);
  350. if (ret)
  351. goto error;
  352. debug("%s: Match found: %s\n", __func__, drv->name);
  353. dev->driver_data = find_id->driver_data;
  354. *devp = dev;
  355. return 0;
  356. }
  357. }
  358. /* Bind a generic driver so that the device can be used */
  359. sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
  360. PCI_FUNC(bdf));
  361. str = strdup(name);
  362. if (!str)
  363. return -ENOMEM;
  364. drv = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI ? "pci_bridge_drv" :
  365. "pci_generic_drv";
  366. ret = device_bind_driver(parent, drv, str, devp);
  367. if (ret) {
  368. debug("%s: Failed to bind generic driver: %d", __func__, ret);
  369. return ret;
  370. }
  371. debug("%s: No match found: bound generic driver instead\n", __func__);
  372. return 0;
  373. error:
  374. debug("%s: No match found: error %d\n", __func__, ret);
  375. return ret;
  376. }
  377. int pci_bind_bus_devices(struct udevice *bus)
  378. {
  379. ulong vendor, device;
  380. ulong header_type;
  381. pci_dev_t bdf, end;
  382. bool found_multi;
  383. int ret;
  384. found_multi = false;
  385. end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
  386. PCI_MAX_PCI_FUNCTIONS - 1);
  387. for (bdf = PCI_BDF(bus->seq, 0, 0); bdf < end;
  388. bdf += PCI_BDF(0, 0, 1)) {
  389. struct pci_child_platdata *pplat;
  390. struct udevice *dev;
  391. ulong class;
  392. if (PCI_FUNC(bdf) && !found_multi)
  393. continue;
  394. /* Check only the first access, we don't expect problems */
  395. ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
  396. &header_type, PCI_SIZE_8);
  397. if (ret)
  398. goto error;
  399. pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
  400. PCI_SIZE_16);
  401. if (vendor == 0xffff || vendor == 0x0000)
  402. continue;
  403. if (!PCI_FUNC(bdf))
  404. found_multi = header_type & 0x80;
  405. debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
  406. bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
  407. pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
  408. PCI_SIZE_16);
  409. pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
  410. PCI_SIZE_32);
  411. class >>= 8;
  412. /* Find this device in the device tree */
  413. ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
  414. /* Search for a driver */
  415. /* If nothing in the device tree, bind a generic device */
  416. if (ret == -ENODEV) {
  417. struct pci_device_id find_id;
  418. ulong val;
  419. memset(&find_id, '\0', sizeof(find_id));
  420. find_id.vendor = vendor;
  421. find_id.device = device;
  422. find_id.class = class;
  423. if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
  424. pci_bus_read_config(bus, bdf,
  425. PCI_SUBSYSTEM_VENDOR_ID,
  426. &val, PCI_SIZE_32);
  427. find_id.subvendor = val & 0xffff;
  428. find_id.subdevice = val >> 16;
  429. }
  430. ret = pci_find_and_bind_driver(bus, &find_id, bdf,
  431. &dev);
  432. }
  433. if (ret)
  434. return ret;
  435. /* Update the platform data */
  436. pplat = dev_get_parent_platdata(dev);
  437. pplat->devfn = PCI_MASK_BUS(bdf);
  438. pplat->vendor = vendor;
  439. pplat->device = device;
  440. pplat->class = class;
  441. }
  442. return 0;
  443. error:
  444. printf("Cannot read bus configuration: %d\n", ret);
  445. return ret;
  446. }
  447. static int pci_uclass_post_bind(struct udevice *bus)
  448. {
  449. /*
  450. * Scan the device tree for devices. This does not probe the PCI bus,
  451. * as this is not permitted while binding. It just finds devices
  452. * mentioned in the device tree.
  453. *
  454. * Before relocation, only bind devices marked for pre-relocation
  455. * use.
  456. */
  457. return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
  458. gd->flags & GD_FLG_RELOC ? false : true);
  459. }
  460. static int decode_regions(struct pci_controller *hose, const void *blob,
  461. int parent_node, int node)
  462. {
  463. int pci_addr_cells, addr_cells, size_cells;
  464. int cells_per_record;
  465. phys_addr_t addr;
  466. const u32 *prop;
  467. int len;
  468. int i;
  469. prop = fdt_getprop(blob, node, "ranges", &len);
  470. if (!prop)
  471. return -EINVAL;
  472. pci_addr_cells = fdt_address_cells(blob, node);
  473. addr_cells = fdt_address_cells(blob, parent_node);
  474. size_cells = fdt_size_cells(blob, node);
  475. /* PCI addresses are always 3-cells */
  476. len /= sizeof(u32);
  477. cells_per_record = pci_addr_cells + addr_cells + size_cells;
  478. hose->region_count = 0;
  479. debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
  480. cells_per_record);
  481. for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
  482. u64 pci_addr, addr, size;
  483. int space_code;
  484. u32 flags;
  485. int type;
  486. if (len < cells_per_record)
  487. break;
  488. flags = fdt32_to_cpu(prop[0]);
  489. space_code = (flags >> 24) & 3;
  490. pci_addr = fdtdec_get_number(prop + 1, 2);
  491. prop += pci_addr_cells;
  492. addr = fdtdec_get_number(prop, addr_cells);
  493. prop += addr_cells;
  494. size = fdtdec_get_number(prop, size_cells);
  495. prop += size_cells;
  496. debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
  497. ", size=%" PRIx64 ", space_code=%d\n", __func__,
  498. hose->region_count, pci_addr, addr, size, space_code);
  499. if (space_code & 2) {
  500. type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
  501. PCI_REGION_MEM;
  502. } else if (space_code & 1) {
  503. type = PCI_REGION_IO;
  504. } else {
  505. continue;
  506. }
  507. debug(" - type=%d\n", type);
  508. pci_set_region(hose->regions + hose->region_count++, pci_addr,
  509. addr, size, type);
  510. }
  511. /* Add a region for our local memory */
  512. addr = gd->ram_size;
  513. if (gd->pci_ram_top && gd->pci_ram_top < addr)
  514. addr = gd->pci_ram_top;
  515. pci_set_region(hose->regions + hose->region_count++, 0, 0, addr,
  516. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  517. return 0;
  518. }
  519. static int pci_uclass_pre_probe(struct udevice *bus)
  520. {
  521. struct pci_controller *hose;
  522. int ret;
  523. debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
  524. bus->parent->name);
  525. hose = bus->uclass_priv;
  526. /* For bridges, use the top-level PCI controller */
  527. if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
  528. hose->ctlr = bus;
  529. ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
  530. bus->of_offset);
  531. if (ret) {
  532. debug("%s: Cannot decode regions\n", __func__);
  533. return ret;
  534. }
  535. } else {
  536. struct pci_controller *parent_hose;
  537. parent_hose = dev_get_uclass_priv(bus->parent);
  538. hose->ctlr = parent_hose->bus;
  539. }
  540. hose->bus = bus;
  541. hose->first_busno = bus->seq;
  542. hose->last_busno = bus->seq;
  543. return 0;
  544. }
  545. static int pci_uclass_post_probe(struct udevice *bus)
  546. {
  547. int ret;
  548. /* Don't scan buses before relocation */
  549. if (!(gd->flags & GD_FLG_RELOC))
  550. return 0;
  551. debug("%s: probing bus %d\n", __func__, bus->seq);
  552. ret = pci_bind_bus_devices(bus);
  553. if (ret)
  554. return ret;
  555. #ifdef CONFIG_PCI_PNP
  556. ret = pci_auto_config_devices(bus);
  557. #endif
  558. return ret < 0 ? ret : 0;
  559. }
  560. static int pci_uclass_child_post_bind(struct udevice *dev)
  561. {
  562. struct pci_child_platdata *pplat;
  563. struct fdt_pci_addr addr;
  564. int ret;
  565. if (dev->of_offset == -1)
  566. return 0;
  567. /*
  568. * We could read vendor, device, class if available. But for now we
  569. * just check the address.
  570. */
  571. pplat = dev_get_parent_platdata(dev);
  572. ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
  573. FDT_PCI_SPACE_CONFIG, "reg", &addr);
  574. if (ret) {
  575. if (ret != -ENOENT)
  576. return -EINVAL;
  577. } else {
  578. /* extract the bdf from fdt_pci_addr */
  579. pplat->devfn = addr.phys_hi & 0xffff00;
  580. }
  581. return 0;
  582. }
  583. static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
  584. uint offset, ulong *valuep,
  585. enum pci_size_t size)
  586. {
  587. struct pci_controller *hose = bus->uclass_priv;
  588. return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
  589. }
  590. static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
  591. uint offset, ulong value,
  592. enum pci_size_t size)
  593. {
  594. struct pci_controller *hose = bus->uclass_priv;
  595. return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
  596. }
  597. UCLASS_DRIVER(pci) = {
  598. .id = UCLASS_PCI,
  599. .name = "pci",
  600. .flags = DM_UC_FLAG_SEQ_ALIAS,
  601. .post_bind = pci_uclass_post_bind,
  602. .pre_probe = pci_uclass_pre_probe,
  603. .post_probe = pci_uclass_post_probe,
  604. .child_post_bind = pci_uclass_child_post_bind,
  605. .per_device_auto_alloc_size = sizeof(struct pci_controller),
  606. .per_child_platdata_auto_alloc_size =
  607. sizeof(struct pci_child_platdata),
  608. };
  609. static const struct dm_pci_ops pci_bridge_ops = {
  610. .read_config = pci_bridge_read_config,
  611. .write_config = pci_bridge_write_config,
  612. };
  613. static const struct udevice_id pci_bridge_ids[] = {
  614. { .compatible = "pci-bridge" },
  615. { }
  616. };
  617. U_BOOT_DRIVER(pci_bridge_drv) = {
  618. .name = "pci_bridge_drv",
  619. .id = UCLASS_PCI,
  620. .of_match = pci_bridge_ids,
  621. .ops = &pci_bridge_ops,
  622. };
  623. UCLASS_DRIVER(pci_generic) = {
  624. .id = UCLASS_PCI_GENERIC,
  625. .name = "pci_generic",
  626. };
  627. static const struct udevice_id pci_generic_ids[] = {
  628. { .compatible = "pci-generic" },
  629. { }
  630. };
  631. U_BOOT_DRIVER(pci_generic_drv) = {
  632. .name = "pci_generic_drv",
  633. .id = UCLASS_PCI_GENERIC,
  634. .of_match = pci_generic_ids,
  635. };