pci-uclass.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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 dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
  177. enum pci_size_t size)
  178. {
  179. struct udevice *bus;
  180. for (bus = dev; device_get_uclass_id(bus->parent) == UCLASS_PCI;)
  181. bus = bus->parent;
  182. return pci_bus_write_config(bus, pci_get_bdf(dev), offset, value, size);
  183. }
  184. int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
  185. {
  186. return pci_write_config(bdf, offset, value, PCI_SIZE_32);
  187. }
  188. int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
  189. {
  190. return pci_write_config(bdf, offset, value, PCI_SIZE_16);
  191. }
  192. int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
  193. {
  194. return pci_write_config(bdf, offset, value, PCI_SIZE_8);
  195. }
  196. int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
  197. {
  198. return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
  199. }
  200. int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
  201. {
  202. return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
  203. }
  204. int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
  205. {
  206. return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
  207. }
  208. int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
  209. unsigned long *valuep, enum pci_size_t size)
  210. {
  211. struct dm_pci_ops *ops;
  212. ops = pci_get_ops(bus);
  213. if (!ops->read_config)
  214. return -ENOSYS;
  215. return ops->read_config(bus, bdf, offset, valuep, size);
  216. }
  217. int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
  218. enum pci_size_t size)
  219. {
  220. struct udevice *bus;
  221. int ret;
  222. ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
  223. if (ret)
  224. return ret;
  225. return pci_bus_read_config(bus, bdf, offset, valuep, size);
  226. }
  227. int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
  228. enum pci_size_t size)
  229. {
  230. struct udevice *bus;
  231. for (bus = dev; device_get_uclass_id(bus->parent) == UCLASS_PCI;)
  232. bus = bus->parent;
  233. return pci_bus_read_config(bus, pci_get_bdf(dev), offset, valuep,
  234. size);
  235. }
  236. int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
  237. {
  238. unsigned long value;
  239. int ret;
  240. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
  241. if (ret)
  242. return ret;
  243. *valuep = value;
  244. return 0;
  245. }
  246. int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
  247. {
  248. unsigned long value;
  249. int ret;
  250. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
  251. if (ret)
  252. return ret;
  253. *valuep = value;
  254. return 0;
  255. }
  256. int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
  257. {
  258. unsigned long value;
  259. int ret;
  260. ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
  261. if (ret)
  262. return ret;
  263. *valuep = value;
  264. return 0;
  265. }
  266. int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
  267. {
  268. unsigned long value;
  269. int ret;
  270. ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
  271. if (ret)
  272. return ret;
  273. *valuep = value;
  274. return 0;
  275. }
  276. int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
  277. {
  278. unsigned long value;
  279. int ret;
  280. ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
  281. if (ret)
  282. return ret;
  283. *valuep = value;
  284. return 0;
  285. }
  286. int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
  287. {
  288. unsigned long value;
  289. int ret;
  290. ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
  291. if (ret)
  292. return ret;
  293. *valuep = value;
  294. return 0;
  295. }
  296. int pci_auto_config_devices(struct udevice *bus)
  297. {
  298. struct pci_controller *hose = bus->uclass_priv;
  299. unsigned int sub_bus;
  300. struct udevice *dev;
  301. int ret;
  302. sub_bus = bus->seq;
  303. debug("%s: start\n", __func__);
  304. pciauto_config_init(hose);
  305. for (ret = device_find_first_child(bus, &dev);
  306. !ret && dev;
  307. ret = device_find_next_child(&dev)) {
  308. unsigned int max_bus;
  309. debug("%s: device %s\n", __func__, dev->name);
  310. max_bus = pciauto_config_device(hose, pci_get_bdf(dev));
  311. sub_bus = max(sub_bus, max_bus);
  312. }
  313. debug("%s: done\n", __func__);
  314. return sub_bus;
  315. }
  316. int dm_pci_hose_probe_bus(struct pci_controller *hose, pci_dev_t bdf)
  317. {
  318. struct udevice *parent, *bus;
  319. int sub_bus;
  320. int ret;
  321. debug("%s\n", __func__);
  322. parent = hose->bus;
  323. /* Find the bus within the parent */
  324. ret = pci_bus_find_devfn(parent, PCI_MASK_BUS(bdf), &bus);
  325. if (ret) {
  326. debug("%s: Cannot find device %x on bus %s: %d\n", __func__,
  327. bdf, parent->name, ret);
  328. return ret;
  329. }
  330. sub_bus = pci_get_bus_max() + 1;
  331. debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
  332. pciauto_prescan_setup_bridge(hose, bdf, sub_bus);
  333. ret = device_probe(bus);
  334. if (ret) {
  335. debug("%s: Cannot probe bus bus %s: %d\n", __func__, bus->name,
  336. ret);
  337. return ret;
  338. }
  339. if (sub_bus != bus->seq) {
  340. printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
  341. __func__, bus->name, bus->seq, sub_bus);
  342. return -EPIPE;
  343. }
  344. sub_bus = pci_get_bus_max();
  345. pciauto_postscan_setup_bridge(hose, bdf, sub_bus);
  346. return sub_bus;
  347. }
  348. /**
  349. * pci_match_one_device - Tell if a PCI device structure has a matching
  350. * PCI device id structure
  351. * @id: single PCI device id structure to match
  352. * @dev: the PCI device structure to match against
  353. *
  354. * Returns the matching pci_device_id structure or %NULL if there is no match.
  355. */
  356. static bool pci_match_one_id(const struct pci_device_id *id,
  357. const struct pci_device_id *find)
  358. {
  359. if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
  360. (id->device == PCI_ANY_ID || id->device == find->device) &&
  361. (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
  362. (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
  363. !((id->class ^ find->class) & id->class_mask))
  364. return true;
  365. return false;
  366. }
  367. /**
  368. * pci_find_and_bind_driver() - Find and bind the right PCI driver
  369. *
  370. * This only looks at certain fields in the descriptor.
  371. */
  372. static int pci_find_and_bind_driver(struct udevice *parent,
  373. struct pci_device_id *find_id, pci_dev_t bdf,
  374. struct udevice **devp)
  375. {
  376. struct pci_driver_entry *start, *entry;
  377. const char *drv;
  378. int n_ents;
  379. int ret;
  380. char name[30], *str;
  381. bool bridge;
  382. *devp = NULL;
  383. debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
  384. find_id->vendor, find_id->device);
  385. start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
  386. n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
  387. for (entry = start; entry != start + n_ents; entry++) {
  388. const struct pci_device_id *id;
  389. struct udevice *dev;
  390. const struct driver *drv;
  391. for (id = entry->match;
  392. id->vendor || id->subvendor || id->class_mask;
  393. id++) {
  394. if (!pci_match_one_id(id, find_id))
  395. continue;
  396. drv = entry->driver;
  397. /*
  398. * In the pre-relocation phase, we only bind devices
  399. * whose driver has the DM_FLAG_PRE_RELOC set, to save
  400. * precious memory space as on some platforms as that
  401. * space is pretty limited (ie: using Cache As RAM).
  402. */
  403. if (!(gd->flags & GD_FLG_RELOC) &&
  404. !(drv->flags & DM_FLAG_PRE_RELOC))
  405. return 0;
  406. /*
  407. * We could pass the descriptor to the driver as
  408. * platdata (instead of NULL) and allow its bind()
  409. * method to return -ENOENT if it doesn't support this
  410. * device. That way we could continue the search to
  411. * find another driver. For now this doesn't seem
  412. * necesssary, so just bind the first match.
  413. */
  414. ret = device_bind(parent, drv, drv->name, NULL, -1,
  415. &dev);
  416. if (ret)
  417. goto error;
  418. debug("%s: Match found: %s\n", __func__, drv->name);
  419. dev->driver_data = find_id->driver_data;
  420. *devp = dev;
  421. return 0;
  422. }
  423. }
  424. bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
  425. /*
  426. * In the pre-relocation phase, we only bind bridge devices to save
  427. * precious memory space as on some platforms as that space is pretty
  428. * limited (ie: using Cache As RAM).
  429. */
  430. if (!(gd->flags & GD_FLG_RELOC) && !bridge)
  431. return 0;
  432. /* Bind a generic driver so that the device can be used */
  433. sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
  434. PCI_FUNC(bdf));
  435. str = strdup(name);
  436. if (!str)
  437. return -ENOMEM;
  438. drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
  439. ret = device_bind_driver(parent, drv, str, devp);
  440. if (ret) {
  441. debug("%s: Failed to bind generic driver: %d", __func__, ret);
  442. return ret;
  443. }
  444. debug("%s: No match found: bound generic driver instead\n", __func__);
  445. return 0;
  446. error:
  447. debug("%s: No match found: error %d\n", __func__, ret);
  448. return ret;
  449. }
  450. int pci_bind_bus_devices(struct udevice *bus)
  451. {
  452. ulong vendor, device;
  453. ulong header_type;
  454. pci_dev_t bdf, end;
  455. bool found_multi;
  456. int ret;
  457. found_multi = false;
  458. end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
  459. PCI_MAX_PCI_FUNCTIONS - 1);
  460. for (bdf = PCI_BDF(bus->seq, 0, 0); bdf < end;
  461. bdf += PCI_BDF(0, 0, 1)) {
  462. struct pci_child_platdata *pplat;
  463. struct udevice *dev;
  464. ulong class;
  465. if (PCI_FUNC(bdf) && !found_multi)
  466. continue;
  467. /* Check only the first access, we don't expect problems */
  468. ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
  469. &header_type, PCI_SIZE_8);
  470. if (ret)
  471. goto error;
  472. pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
  473. PCI_SIZE_16);
  474. if (vendor == 0xffff || vendor == 0x0000)
  475. continue;
  476. if (!PCI_FUNC(bdf))
  477. found_multi = header_type & 0x80;
  478. debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
  479. bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
  480. pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
  481. PCI_SIZE_16);
  482. pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
  483. PCI_SIZE_32);
  484. class >>= 8;
  485. /* Find this device in the device tree */
  486. ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
  487. /* Search for a driver */
  488. /* If nothing in the device tree, bind a generic device */
  489. if (ret == -ENODEV) {
  490. struct pci_device_id find_id;
  491. ulong val;
  492. memset(&find_id, '\0', sizeof(find_id));
  493. find_id.vendor = vendor;
  494. find_id.device = device;
  495. find_id.class = class;
  496. if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
  497. pci_bus_read_config(bus, bdf,
  498. PCI_SUBSYSTEM_VENDOR_ID,
  499. &val, PCI_SIZE_32);
  500. find_id.subvendor = val & 0xffff;
  501. find_id.subdevice = val >> 16;
  502. }
  503. ret = pci_find_and_bind_driver(bus, &find_id, bdf,
  504. &dev);
  505. }
  506. if (ret)
  507. return ret;
  508. /* Update the platform data */
  509. if (dev) {
  510. pplat = dev_get_parent_platdata(dev);
  511. pplat->devfn = PCI_MASK_BUS(bdf);
  512. pplat->vendor = vendor;
  513. pplat->device = device;
  514. pplat->class = class;
  515. }
  516. }
  517. return 0;
  518. error:
  519. printf("Cannot read bus configuration: %d\n", ret);
  520. return ret;
  521. }
  522. static int pci_uclass_post_bind(struct udevice *bus)
  523. {
  524. /*
  525. * Scan the device tree for devices. This does not probe the PCI bus,
  526. * as this is not permitted while binding. It just finds devices
  527. * mentioned in the device tree.
  528. *
  529. * Before relocation, only bind devices marked for pre-relocation
  530. * use.
  531. */
  532. return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
  533. gd->flags & GD_FLG_RELOC ? false : true);
  534. }
  535. static int decode_regions(struct pci_controller *hose, const void *blob,
  536. int parent_node, int node)
  537. {
  538. int pci_addr_cells, addr_cells, size_cells;
  539. int cells_per_record;
  540. phys_addr_t addr;
  541. const u32 *prop;
  542. int len;
  543. int i;
  544. prop = fdt_getprop(blob, node, "ranges", &len);
  545. if (!prop)
  546. return -EINVAL;
  547. pci_addr_cells = fdt_address_cells(blob, node);
  548. addr_cells = fdt_address_cells(blob, parent_node);
  549. size_cells = fdt_size_cells(blob, node);
  550. /* PCI addresses are always 3-cells */
  551. len /= sizeof(u32);
  552. cells_per_record = pci_addr_cells + addr_cells + size_cells;
  553. hose->region_count = 0;
  554. debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
  555. cells_per_record);
  556. for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
  557. u64 pci_addr, addr, size;
  558. int space_code;
  559. u32 flags;
  560. int type;
  561. if (len < cells_per_record)
  562. break;
  563. flags = fdt32_to_cpu(prop[0]);
  564. space_code = (flags >> 24) & 3;
  565. pci_addr = fdtdec_get_number(prop + 1, 2);
  566. prop += pci_addr_cells;
  567. addr = fdtdec_get_number(prop, addr_cells);
  568. prop += addr_cells;
  569. size = fdtdec_get_number(prop, size_cells);
  570. prop += size_cells;
  571. debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
  572. ", size=%" PRIx64 ", space_code=%d\n", __func__,
  573. hose->region_count, pci_addr, addr, size, space_code);
  574. if (space_code & 2) {
  575. type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
  576. PCI_REGION_MEM;
  577. } else if (space_code & 1) {
  578. type = PCI_REGION_IO;
  579. } else {
  580. continue;
  581. }
  582. debug(" - type=%d\n", type);
  583. pci_set_region(hose->regions + hose->region_count++, pci_addr,
  584. addr, size, type);
  585. }
  586. /* Add a region for our local memory */
  587. addr = gd->ram_size;
  588. if (gd->pci_ram_top && gd->pci_ram_top < addr)
  589. addr = gd->pci_ram_top;
  590. pci_set_region(hose->regions + hose->region_count++, 0, 0, addr,
  591. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  592. return 0;
  593. }
  594. static int pci_uclass_pre_probe(struct udevice *bus)
  595. {
  596. struct pci_controller *hose;
  597. int ret;
  598. debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
  599. bus->parent->name);
  600. hose = bus->uclass_priv;
  601. /* For bridges, use the top-level PCI controller */
  602. if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
  603. hose->ctlr = bus;
  604. ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
  605. bus->of_offset);
  606. if (ret) {
  607. debug("%s: Cannot decode regions\n", __func__);
  608. return ret;
  609. }
  610. } else {
  611. struct pci_controller *parent_hose;
  612. parent_hose = dev_get_uclass_priv(bus->parent);
  613. hose->ctlr = parent_hose->bus;
  614. }
  615. hose->bus = bus;
  616. hose->first_busno = bus->seq;
  617. hose->last_busno = bus->seq;
  618. return 0;
  619. }
  620. static int pci_uclass_post_probe(struct udevice *bus)
  621. {
  622. int ret;
  623. debug("%s: probing bus %d\n", __func__, bus->seq);
  624. ret = pci_bind_bus_devices(bus);
  625. if (ret)
  626. return ret;
  627. #ifdef CONFIG_PCI_PNP
  628. ret = pci_auto_config_devices(bus);
  629. #endif
  630. return ret < 0 ? ret : 0;
  631. }
  632. static int pci_uclass_child_post_bind(struct udevice *dev)
  633. {
  634. struct pci_child_platdata *pplat;
  635. struct fdt_pci_addr addr;
  636. int ret;
  637. if (dev->of_offset == -1)
  638. return 0;
  639. /*
  640. * We could read vendor, device, class if available. But for now we
  641. * just check the address.
  642. */
  643. pplat = dev_get_parent_platdata(dev);
  644. ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
  645. FDT_PCI_SPACE_CONFIG, "reg", &addr);
  646. if (ret) {
  647. if (ret != -ENOENT)
  648. return -EINVAL;
  649. } else {
  650. /* extract the bdf from fdt_pci_addr */
  651. pplat->devfn = addr.phys_hi & 0xffff00;
  652. }
  653. return 0;
  654. }
  655. static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
  656. uint offset, ulong *valuep,
  657. enum pci_size_t size)
  658. {
  659. struct pci_controller *hose = bus->uclass_priv;
  660. return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
  661. }
  662. static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
  663. uint offset, ulong value,
  664. enum pci_size_t size)
  665. {
  666. struct pci_controller *hose = bus->uclass_priv;
  667. return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
  668. }
  669. static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
  670. {
  671. struct udevice *dev;
  672. int ret = 0;
  673. /*
  674. * Scan through all the PCI controllers. On x86 there will only be one
  675. * but that is not necessarily true on other hardware.
  676. */
  677. do {
  678. device_find_first_child(bus, &dev);
  679. if (dev) {
  680. *devp = dev;
  681. return 0;
  682. }
  683. ret = uclass_next_device(&bus);
  684. if (ret)
  685. return ret;
  686. } while (bus);
  687. return 0;
  688. }
  689. int pci_find_next_device(struct udevice **devp)
  690. {
  691. struct udevice *child = *devp;
  692. struct udevice *bus = child->parent;
  693. int ret;
  694. /* First try all the siblings */
  695. *devp = NULL;
  696. while (child) {
  697. device_find_next_child(&child);
  698. if (child) {
  699. *devp = child;
  700. return 0;
  701. }
  702. }
  703. /* We ran out of siblings. Try the next bus */
  704. ret = uclass_next_device(&bus);
  705. if (ret)
  706. return ret;
  707. return bus ? skip_to_next_device(bus, devp) : 0;
  708. }
  709. int pci_find_first_device(struct udevice **devp)
  710. {
  711. struct udevice *bus;
  712. int ret;
  713. *devp = NULL;
  714. ret = uclass_first_device(UCLASS_PCI, &bus);
  715. if (ret)
  716. return ret;
  717. return skip_to_next_device(bus, devp);
  718. }
  719. UCLASS_DRIVER(pci) = {
  720. .id = UCLASS_PCI,
  721. .name = "pci",
  722. .flags = DM_UC_FLAG_SEQ_ALIAS,
  723. .post_bind = pci_uclass_post_bind,
  724. .pre_probe = pci_uclass_pre_probe,
  725. .post_probe = pci_uclass_post_probe,
  726. .child_post_bind = pci_uclass_child_post_bind,
  727. .per_device_auto_alloc_size = sizeof(struct pci_controller),
  728. .per_child_platdata_auto_alloc_size =
  729. sizeof(struct pci_child_platdata),
  730. };
  731. static const struct dm_pci_ops pci_bridge_ops = {
  732. .read_config = pci_bridge_read_config,
  733. .write_config = pci_bridge_write_config,
  734. };
  735. static const struct udevice_id pci_bridge_ids[] = {
  736. { .compatible = "pci-bridge" },
  737. { }
  738. };
  739. U_BOOT_DRIVER(pci_bridge_drv) = {
  740. .name = "pci_bridge_drv",
  741. .id = UCLASS_PCI,
  742. .of_match = pci_bridge_ids,
  743. .ops = &pci_bridge_ops,
  744. };
  745. UCLASS_DRIVER(pci_generic) = {
  746. .id = UCLASS_PCI_GENERIC,
  747. .name = "pci_generic",
  748. };
  749. static const struct udevice_id pci_generic_ids[] = {
  750. { .compatible = "pci-generic" },
  751. { }
  752. };
  753. U_BOOT_DRIVER(pci_generic_drv) = {
  754. .name = "pci_generic_drv",
  755. .id = UCLASS_PCI_GENERIC,
  756. .of_match = pci_generic_ids,
  757. };