pci-uclass.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. /**
  29. * pci_get_bus_max() - returns the bus number of the last active bus
  30. *
  31. * @return last bus number, or -1 if no active buses
  32. */
  33. static int pci_get_bus_max(void)
  34. {
  35. struct udevice *bus;
  36. struct uclass *uc;
  37. int ret = -1;
  38. ret = uclass_get(UCLASS_PCI, &uc);
  39. uclass_foreach_dev(bus, uc) {
  40. if (bus->seq > ret)
  41. ret = bus->seq;
  42. }
  43. debug("%s: ret=%d\n", __func__, ret);
  44. return ret;
  45. }
  46. int pci_last_busno(void)
  47. {
  48. struct pci_controller *hose;
  49. struct udevice *bus;
  50. struct uclass *uc;
  51. int ret;
  52. debug("pci_last_busno\n");
  53. ret = uclass_get(UCLASS_PCI, &uc);
  54. if (ret || list_empty(&uc->dev_head))
  55. return -1;
  56. /* Probe the last bus */
  57. bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
  58. debug("bus = %p, %s\n", bus, bus->name);
  59. assert(bus);
  60. ret = device_probe(bus);
  61. if (ret)
  62. return ret;
  63. /* If that bus has bridges, we may have new buses now. Get the last */
  64. bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
  65. hose = dev_get_uclass_priv(bus);
  66. debug("bus = %s, hose = %p\n", bus->name, hose);
  67. return hose->last_busno;
  68. }
  69. int pci_get_ff(enum pci_size_t size)
  70. {
  71. switch (size) {
  72. case PCI_SIZE_8:
  73. return 0xff;
  74. case PCI_SIZE_16:
  75. return 0xffff;
  76. default:
  77. return 0xffffffff;
  78. }
  79. }
  80. int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
  81. struct udevice **devp)
  82. {
  83. struct udevice *dev;
  84. for (device_find_first_child(bus, &dev);
  85. dev;
  86. device_find_next_child(&dev)) {
  87. struct pci_child_platdata *pplat;
  88. pplat = dev_get_parent_platdata(dev);
  89. if (pplat && pplat->devfn == find_devfn) {
  90. *devp = dev;
  91. return 0;
  92. }
  93. }
  94. return -ENODEV;
  95. }
  96. int pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
  97. {
  98. struct udevice *bus;
  99. int ret;
  100. ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
  101. if (ret)
  102. return ret;
  103. return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
  104. }
  105. static int pci_device_matches_ids(struct udevice *dev,
  106. struct pci_device_id *ids)
  107. {
  108. struct pci_child_platdata *pplat;
  109. int i;
  110. pplat = dev_get_parent_platdata(dev);
  111. if (!pplat)
  112. return -EINVAL;
  113. for (i = 0; ids[i].vendor != 0; i++) {
  114. if (pplat->vendor == ids[i].vendor &&
  115. pplat->device == ids[i].device)
  116. return i;
  117. }
  118. return -EINVAL;
  119. }
  120. int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
  121. int *indexp, struct udevice **devp)
  122. {
  123. struct udevice *dev;
  124. /* Scan all devices on this bus */
  125. for (device_find_first_child(bus, &dev);
  126. dev;
  127. device_find_next_child(&dev)) {
  128. if (pci_device_matches_ids(dev, ids) >= 0) {
  129. if ((*indexp)-- <= 0) {
  130. *devp = dev;
  131. return 0;
  132. }
  133. }
  134. }
  135. return -ENODEV;
  136. }
  137. int pci_find_device_id(struct pci_device_id *ids, int index,
  138. struct udevice **devp)
  139. {
  140. struct udevice *bus;
  141. /* Scan all known buses */
  142. for (uclass_first_device(UCLASS_PCI, &bus);
  143. bus;
  144. uclass_next_device(&bus)) {
  145. if (!pci_bus_find_devices(bus, ids, &index, devp))
  146. return 0;
  147. }
  148. *devp = NULL;
  149. return -ENODEV;
  150. }
  151. int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
  152. unsigned long value, enum pci_size_t size)
  153. {
  154. struct dm_pci_ops *ops;
  155. ops = pci_get_ops(bus);
  156. if (!ops->write_config)
  157. return -ENOSYS;
  158. return ops->write_config(bus, bdf, offset, value, size);
  159. }
  160. int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
  161. enum pci_size_t size)
  162. {
  163. struct udevice *bus;
  164. int ret;
  165. ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
  166. if (ret)
  167. return ret;
  168. return pci_bus_write_config(bus, PCI_MASK_BUS(bdf), offset, value,
  169. size);
  170. }
  171. int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
  172. {
  173. return pci_write_config(bdf, offset, value, PCI_SIZE_32);
  174. }
  175. int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
  176. {
  177. return pci_write_config(bdf, offset, value, PCI_SIZE_16);
  178. }
  179. int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
  180. {
  181. return pci_write_config(bdf, offset, value, PCI_SIZE_8);
  182. }
  183. int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
  184. unsigned long *valuep, enum pci_size_t size)
  185. {
  186. struct dm_pci_ops *ops;
  187. ops = pci_get_ops(bus);
  188. if (!ops->read_config)
  189. return -ENOSYS;
  190. return ops->read_config(bus, bdf, offset, valuep, size);
  191. }
  192. int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
  193. enum pci_size_t size)
  194. {
  195. struct udevice *bus;
  196. int ret;
  197. ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
  198. if (ret)
  199. return ret;
  200. return pci_bus_read_config(bus, PCI_MASK_BUS(bdf), offset, valuep,
  201. size);
  202. }
  203. int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
  204. {
  205. unsigned long value;
  206. int ret;
  207. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
  208. if (ret)
  209. return ret;
  210. *valuep = value;
  211. return 0;
  212. }
  213. int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
  214. {
  215. unsigned long value;
  216. int ret;
  217. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
  218. if (ret)
  219. return ret;
  220. *valuep = value;
  221. return 0;
  222. }
  223. int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
  224. {
  225. unsigned long value;
  226. int ret;
  227. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
  228. if (ret)
  229. return ret;
  230. *valuep = value;
  231. return 0;
  232. }
  233. int pci_auto_config_devices(struct udevice *bus)
  234. {
  235. struct pci_controller *hose = bus->uclass_priv;
  236. unsigned int sub_bus;
  237. struct udevice *dev;
  238. int ret;
  239. sub_bus = bus->seq;
  240. debug("%s: start\n", __func__);
  241. pciauto_config_init(hose);
  242. for (ret = device_find_first_child(bus, &dev);
  243. !ret && dev;
  244. ret = device_find_next_child(&dev)) {
  245. struct pci_child_platdata *pplat;
  246. struct pci_controller *ctlr_hose;
  247. pplat = dev_get_parent_platdata(dev);
  248. unsigned int max_bus;
  249. pci_dev_t bdf;
  250. bdf = PCI_ADD_BUS(bus->seq, pplat->devfn);
  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, bdf);
  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, 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, int devfn,
  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(devfn),
  360. PCI_FUNC(devfn));
  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 devfn, end;
  382. bool found_multi;
  383. int ret;
  384. found_multi = false;
  385. end = PCI_DEVFN(PCI_MAX_PCI_DEVICES - 1, PCI_MAX_PCI_FUNCTIONS - 1);
  386. for (devfn = PCI_DEVFN(0, 0); devfn < end; devfn += PCI_DEVFN(0, 1)) {
  387. struct pci_child_platdata *pplat;
  388. struct udevice *dev;
  389. ulong class;
  390. if (PCI_FUNC(devfn) && !found_multi)
  391. continue;
  392. /* Check only the first access, we don't expect problems */
  393. ret = pci_bus_read_config(bus, devfn, PCI_HEADER_TYPE,
  394. &header_type, PCI_SIZE_8);
  395. if (ret)
  396. goto error;
  397. pci_bus_read_config(bus, devfn, PCI_VENDOR_ID, &vendor,
  398. PCI_SIZE_16);
  399. if (vendor == 0xffff || vendor == 0x0000)
  400. continue;
  401. if (!PCI_FUNC(devfn))
  402. found_multi = header_type & 0x80;
  403. debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
  404. bus->seq, bus->name, PCI_DEV(devfn), PCI_FUNC(devfn));
  405. pci_bus_read_config(bus, devfn, PCI_DEVICE_ID, &device,
  406. PCI_SIZE_16);
  407. pci_bus_read_config(bus, devfn, PCI_CLASS_REVISION, &class,
  408. PCI_SIZE_32);
  409. class >>= 8;
  410. /* Find this device in the device tree */
  411. ret = pci_bus_find_devfn(bus, devfn, &dev);
  412. /* Search for a driver */
  413. /* If nothing in the device tree, bind a generic device */
  414. if (ret == -ENODEV) {
  415. struct pci_device_id find_id;
  416. ulong val;
  417. memset(&find_id, '\0', sizeof(find_id));
  418. find_id.vendor = vendor;
  419. find_id.device = device;
  420. find_id.class = class;
  421. if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
  422. pci_bus_read_config(bus, devfn,
  423. PCI_SUBSYSTEM_VENDOR_ID,
  424. &val, PCI_SIZE_32);
  425. find_id.subvendor = val & 0xffff;
  426. find_id.subdevice = val >> 16;
  427. }
  428. ret = pci_find_and_bind_driver(bus, &find_id, devfn,
  429. &dev);
  430. }
  431. if (ret)
  432. return ret;
  433. /* Update the platform data */
  434. pplat = dev_get_parent_platdata(dev);
  435. pplat->devfn = devfn;
  436. pplat->vendor = vendor;
  437. pplat->device = device;
  438. pplat->class = class;
  439. }
  440. return 0;
  441. error:
  442. printf("Cannot read bus configuration: %d\n", ret);
  443. return ret;
  444. }
  445. static int pci_uclass_post_bind(struct udevice *bus)
  446. {
  447. /*
  448. * Scan the device tree for devices. This does not probe the PCI bus,
  449. * as this is not permitted while binding. It just finds devices
  450. * mentioned in the device tree.
  451. *
  452. * Before relocation, only bind devices marked for pre-relocation
  453. * use.
  454. */
  455. return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
  456. gd->flags & GD_FLG_RELOC ? false : true);
  457. }
  458. static int decode_regions(struct pci_controller *hose, const void *blob,
  459. int parent_node, int node)
  460. {
  461. int pci_addr_cells, addr_cells, size_cells;
  462. int cells_per_record;
  463. phys_addr_t addr;
  464. const u32 *prop;
  465. int len;
  466. int i;
  467. prop = fdt_getprop(blob, node, "ranges", &len);
  468. if (!prop)
  469. return -EINVAL;
  470. pci_addr_cells = fdt_address_cells(blob, node);
  471. addr_cells = fdt_address_cells(blob, parent_node);
  472. size_cells = fdt_size_cells(blob, node);
  473. /* PCI addresses are always 3-cells */
  474. len /= sizeof(u32);
  475. cells_per_record = pci_addr_cells + addr_cells + size_cells;
  476. hose->region_count = 0;
  477. debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
  478. cells_per_record);
  479. for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
  480. u64 pci_addr, addr, size;
  481. int space_code;
  482. u32 flags;
  483. int type;
  484. if (len < cells_per_record)
  485. break;
  486. flags = fdt32_to_cpu(prop[0]);
  487. space_code = (flags >> 24) & 3;
  488. pci_addr = fdtdec_get_number(prop + 1, 2);
  489. prop += pci_addr_cells;
  490. addr = fdtdec_get_number(prop, addr_cells);
  491. prop += addr_cells;
  492. size = fdtdec_get_number(prop, size_cells);
  493. prop += size_cells;
  494. debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
  495. ", size=%" PRIx64 ", space_code=%d\n", __func__,
  496. hose->region_count, pci_addr, addr, size, space_code);
  497. if (space_code & 2) {
  498. type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
  499. PCI_REGION_MEM;
  500. } else if (space_code & 1) {
  501. type = PCI_REGION_IO;
  502. } else {
  503. continue;
  504. }
  505. debug(" - type=%d\n", type);
  506. pci_set_region(hose->regions + hose->region_count++, pci_addr,
  507. addr, size, type);
  508. }
  509. /* Add a region for our local memory */
  510. addr = gd->ram_size;
  511. if (gd->pci_ram_top && gd->pci_ram_top < addr)
  512. addr = gd->pci_ram_top;
  513. pci_set_region(hose->regions + hose->region_count++, 0, 0, addr,
  514. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  515. return 0;
  516. }
  517. static int pci_uclass_pre_probe(struct udevice *bus)
  518. {
  519. struct pci_controller *hose;
  520. int ret;
  521. debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
  522. bus->parent->name);
  523. hose = bus->uclass_priv;
  524. /* For bridges, use the top-level PCI controller */
  525. if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
  526. hose->ctlr = bus;
  527. ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
  528. bus->of_offset);
  529. if (ret) {
  530. debug("%s: Cannot decode regions\n", __func__);
  531. return ret;
  532. }
  533. } else {
  534. struct pci_controller *parent_hose;
  535. parent_hose = dev_get_uclass_priv(bus->parent);
  536. hose->ctlr = parent_hose->bus;
  537. }
  538. hose->bus = bus;
  539. hose->first_busno = bus->seq;
  540. hose->last_busno = bus->seq;
  541. return 0;
  542. }
  543. static int pci_uclass_post_probe(struct udevice *bus)
  544. {
  545. int ret;
  546. /* Don't scan buses before relocation */
  547. if (!(gd->flags & GD_FLG_RELOC))
  548. return 0;
  549. debug("%s: probing bus %d\n", __func__, bus->seq);
  550. ret = pci_bind_bus_devices(bus);
  551. if (ret)
  552. return ret;
  553. #ifdef CONFIG_PCI_PNP
  554. ret = pci_auto_config_devices(bus);
  555. #endif
  556. return ret < 0 ? ret : 0;
  557. }
  558. static int pci_uclass_child_post_bind(struct udevice *dev)
  559. {
  560. struct pci_child_platdata *pplat;
  561. struct fdt_pci_addr addr;
  562. int ret;
  563. if (dev->of_offset == -1)
  564. return 0;
  565. /*
  566. * We could read vendor, device, class if available. But for now we
  567. * just check the address.
  568. */
  569. pplat = dev_get_parent_platdata(dev);
  570. ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
  571. FDT_PCI_SPACE_CONFIG, "reg", &addr);
  572. if (ret) {
  573. if (ret != -ENOENT)
  574. return -EINVAL;
  575. } else {
  576. /* extract the bdf from fdt_pci_addr */
  577. pplat->devfn = addr.phys_hi & 0xffff00;
  578. }
  579. return 0;
  580. }
  581. int pci_bridge_read_config(struct udevice *bus, pci_dev_t devfn, uint offset,
  582. ulong *valuep, enum pci_size_t size)
  583. {
  584. struct pci_controller *hose = bus->uclass_priv;
  585. pci_dev_t bdf = PCI_ADD_BUS(bus->seq, devfn);
  586. return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
  587. }
  588. int pci_bridge_write_config(struct udevice *bus, pci_dev_t devfn, uint offset,
  589. ulong value, enum pci_size_t size)
  590. {
  591. struct pci_controller *hose = bus->uclass_priv;
  592. pci_dev_t bdf = PCI_ADD_BUS(bus->seq, devfn);
  593. return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
  594. }
  595. UCLASS_DRIVER(pci) = {
  596. .id = UCLASS_PCI,
  597. .name = "pci",
  598. .flags = DM_UC_FLAG_SEQ_ALIAS,
  599. .post_bind = pci_uclass_post_bind,
  600. .pre_probe = pci_uclass_pre_probe,
  601. .post_probe = pci_uclass_post_probe,
  602. .child_post_bind = pci_uclass_child_post_bind,
  603. .per_device_auto_alloc_size = sizeof(struct pci_controller),
  604. .per_child_platdata_auto_alloc_size =
  605. sizeof(struct pci_child_platdata),
  606. };
  607. static const struct dm_pci_ops pci_bridge_ops = {
  608. .read_config = pci_bridge_read_config,
  609. .write_config = pci_bridge_write_config,
  610. };
  611. static const struct udevice_id pci_bridge_ids[] = {
  612. { .compatible = "pci-bridge" },
  613. { }
  614. };
  615. U_BOOT_DRIVER(pci_bridge_drv) = {
  616. .name = "pci_bridge_drv",
  617. .id = UCLASS_PCI,
  618. .of_match = pci_bridge_ids,
  619. .ops = &pci_bridge_ops,
  620. };
  621. UCLASS_DRIVER(pci_generic) = {
  622. .id = UCLASS_PCI_GENERIC,
  623. .name = "pci_generic",
  624. };
  625. static const struct udevice_id pci_generic_ids[] = {
  626. { .compatible = "pci-generic" },
  627. { }
  628. };
  629. U_BOOT_DRIVER(pci_generic_drv) = {
  630. .name = "pci_generic_drv",
  631. .id = UCLASS_PCI_GENERIC,
  632. .of_match = pci_generic_ids,
  633. };