of.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI <-> OF mapping helpers
  4. *
  5. * Copyright 2011 IBM Corp.
  6. */
  7. #define pr_fmt(fmt) "PCI: OF: " fmt
  8. #include <linux/irqdomain.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pci.h>
  11. #include <linux/of.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_pci.h>
  15. #include "pci.h"
  16. #ifdef CONFIG_PCI
  17. void pci_set_of_node(struct pci_dev *dev)
  18. {
  19. if (!dev->bus->dev.of_node)
  20. return;
  21. dev->dev.of_node = of_pci_find_child_device(dev->bus->dev.of_node,
  22. dev->devfn);
  23. if (dev->dev.of_node)
  24. dev->dev.fwnode = &dev->dev.of_node->fwnode;
  25. }
  26. void pci_release_of_node(struct pci_dev *dev)
  27. {
  28. of_node_put(dev->dev.of_node);
  29. dev->dev.of_node = NULL;
  30. dev->dev.fwnode = NULL;
  31. }
  32. void pci_set_bus_of_node(struct pci_bus *bus)
  33. {
  34. struct device_node *node;
  35. if (bus->self == NULL) {
  36. node = pcibios_get_phb_of_node(bus);
  37. } else {
  38. node = of_node_get(bus->self->dev.of_node);
  39. if (node && of_property_read_bool(node, "external-facing"))
  40. bus->self->external_facing = true;
  41. }
  42. bus->dev.of_node = node;
  43. if (bus->dev.of_node)
  44. bus->dev.fwnode = &bus->dev.of_node->fwnode;
  45. }
  46. void pci_release_bus_of_node(struct pci_bus *bus)
  47. {
  48. of_node_put(bus->dev.of_node);
  49. bus->dev.of_node = NULL;
  50. bus->dev.fwnode = NULL;
  51. }
  52. struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus)
  53. {
  54. /* This should only be called for PHBs */
  55. if (WARN_ON(bus->self || bus->parent))
  56. return NULL;
  57. /*
  58. * Look for a node pointer in either the intermediary device we
  59. * create above the root bus or its own parent. Normally only
  60. * the later is populated.
  61. */
  62. if (bus->bridge->of_node)
  63. return of_node_get(bus->bridge->of_node);
  64. if (bus->bridge->parent && bus->bridge->parent->of_node)
  65. return of_node_get(bus->bridge->parent->of_node);
  66. return NULL;
  67. }
  68. struct irq_domain *pci_host_bridge_of_msi_domain(struct pci_bus *bus)
  69. {
  70. #ifdef CONFIG_IRQ_DOMAIN
  71. struct irq_domain *d;
  72. if (!bus->dev.of_node)
  73. return NULL;
  74. /* Start looking for a phandle to an MSI controller. */
  75. d = of_msi_get_domain(&bus->dev, bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
  76. if (d)
  77. return d;
  78. /*
  79. * If we don't have an msi-parent property, look for a domain
  80. * directly attached to the host bridge.
  81. */
  82. d = irq_find_matching_host(bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
  83. if (d)
  84. return d;
  85. return irq_find_host(bus->dev.of_node);
  86. #else
  87. return NULL;
  88. #endif
  89. }
  90. bool pci_host_of_has_msi_map(struct device *dev)
  91. {
  92. if (dev && dev->of_node)
  93. return of_get_property(dev->of_node, "msi-map", NULL);
  94. return false;
  95. }
  96. static inline int __of_pci_pci_compare(struct device_node *node,
  97. unsigned int data)
  98. {
  99. int devfn;
  100. devfn = of_pci_get_devfn(node);
  101. if (devfn < 0)
  102. return 0;
  103. return devfn == data;
  104. }
  105. struct device_node *of_pci_find_child_device(struct device_node *parent,
  106. unsigned int devfn)
  107. {
  108. struct device_node *node, *node2;
  109. for_each_child_of_node(parent, node) {
  110. if (__of_pci_pci_compare(node, devfn))
  111. return node;
  112. /*
  113. * Some OFs create a parent node "multifunc-device" as
  114. * a fake root for all functions of a multi-function
  115. * device we go down them as well.
  116. */
  117. if (of_node_name_eq(node, "multifunc-device")) {
  118. for_each_child_of_node(node, node2) {
  119. if (__of_pci_pci_compare(node2, devfn)) {
  120. of_node_put(node);
  121. return node2;
  122. }
  123. }
  124. }
  125. }
  126. return NULL;
  127. }
  128. EXPORT_SYMBOL_GPL(of_pci_find_child_device);
  129. /**
  130. * of_pci_get_devfn() - Get device and function numbers for a device node
  131. * @np: device node
  132. *
  133. * Parses a standard 5-cell PCI resource and returns an 8-bit value that can
  134. * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device
  135. * and function numbers respectively. On error a negative error code is
  136. * returned.
  137. */
  138. int of_pci_get_devfn(struct device_node *np)
  139. {
  140. u32 reg[5];
  141. int error;
  142. error = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg));
  143. if (error)
  144. return error;
  145. return (reg[0] >> 8) & 0xff;
  146. }
  147. EXPORT_SYMBOL_GPL(of_pci_get_devfn);
  148. /**
  149. * of_pci_parse_bus_range() - parse the bus-range property of a PCI device
  150. * @node: device node
  151. * @res: address to a struct resource to return the bus-range
  152. *
  153. * Returns 0 on success or a negative error-code on failure.
  154. */
  155. int of_pci_parse_bus_range(struct device_node *node, struct resource *res)
  156. {
  157. u32 bus_range[2];
  158. int error;
  159. error = of_property_read_u32_array(node, "bus-range", bus_range,
  160. ARRAY_SIZE(bus_range));
  161. if (error)
  162. return error;
  163. res->name = node->name;
  164. res->start = bus_range[0];
  165. res->end = bus_range[1];
  166. res->flags = IORESOURCE_BUS;
  167. return 0;
  168. }
  169. EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
  170. /**
  171. * of_get_pci_domain_nr - Find the host bridge domain number
  172. * of the given device node.
  173. * @node: Device tree node with the domain information.
  174. *
  175. * This function will try to obtain the host bridge domain number by finding
  176. * a property called "linux,pci-domain" of the given device node.
  177. *
  178. * Return:
  179. * * > 0 - On success, an associated domain number.
  180. * * -EINVAL - The property "linux,pci-domain" does not exist.
  181. * * -ENODATA - The linux,pci-domain" property does not have value.
  182. * * -EOVERFLOW - Invalid "linux,pci-domain" property value.
  183. *
  184. * Returns the associated domain number from DT in the range [0-0xffff], or
  185. * a negative value if the required property is not found.
  186. */
  187. int of_get_pci_domain_nr(struct device_node *node)
  188. {
  189. u32 domain;
  190. int error;
  191. error = of_property_read_u32(node, "linux,pci-domain", &domain);
  192. if (error)
  193. return error;
  194. return (u16)domain;
  195. }
  196. EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
  197. /**
  198. * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only
  199. * is present and valid
  200. */
  201. void of_pci_check_probe_only(void)
  202. {
  203. u32 val;
  204. int ret;
  205. ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val);
  206. if (ret) {
  207. if (ret == -ENODATA || ret == -EOVERFLOW)
  208. pr_warn("linux,pci-probe-only without valid value, ignoring\n");
  209. return;
  210. }
  211. if (val)
  212. pci_add_flags(PCI_PROBE_ONLY);
  213. else
  214. pci_clear_flags(PCI_PROBE_ONLY);
  215. pr_info("PROBE_ONLY %s\n", val ? "enabled" : "disabled");
  216. }
  217. EXPORT_SYMBOL_GPL(of_pci_check_probe_only);
  218. /**
  219. * devm_of_pci_get_host_bridge_resources() - Resource-managed parsing of PCI
  220. * host bridge resources from DT
  221. * @dev: host bridge device
  222. * @busno: bus number associated with the bridge root bus
  223. * @bus_max: maximum number of buses for this bridge
  224. * @resources: list where the range of resources will be added after DT parsing
  225. * @ib_resources: list where the range of inbound resources (with addresses
  226. * from 'dma-ranges') will be added after DT parsing
  227. * @io_base: pointer to a variable that will contain on return the physical
  228. * address for the start of the I/O range. Can be NULL if the caller doesn't
  229. * expect I/O ranges to be present in the device tree.
  230. *
  231. * This function will parse the "ranges" property of a PCI host bridge device
  232. * node and setup the resource mapping based on its content. It is expected
  233. * that the property conforms with the Power ePAPR document.
  234. *
  235. * It returns zero if the range parsing has been successful or a standard error
  236. * value if it failed.
  237. */
  238. static int devm_of_pci_get_host_bridge_resources(struct device *dev,
  239. unsigned char busno, unsigned char bus_max,
  240. struct list_head *resources,
  241. struct list_head *ib_resources,
  242. resource_size_t *io_base)
  243. {
  244. struct device_node *dev_node = dev->of_node;
  245. struct resource *res, tmp_res;
  246. struct resource *bus_range;
  247. struct of_pci_range range;
  248. struct of_pci_range_parser parser;
  249. const char *range_type;
  250. int err;
  251. if (io_base)
  252. *io_base = (resource_size_t)OF_BAD_ADDR;
  253. bus_range = devm_kzalloc(dev, sizeof(*bus_range), GFP_KERNEL);
  254. if (!bus_range)
  255. return -ENOMEM;
  256. dev_info(dev, "host bridge %pOF ranges:\n", dev_node);
  257. err = of_pci_parse_bus_range(dev_node, bus_range);
  258. if (err) {
  259. bus_range->start = busno;
  260. bus_range->end = bus_max;
  261. bus_range->flags = IORESOURCE_BUS;
  262. dev_info(dev, " No bus range found for %pOF, using %pR\n",
  263. dev_node, bus_range);
  264. } else {
  265. if (bus_range->end > bus_range->start + bus_max)
  266. bus_range->end = bus_range->start + bus_max;
  267. }
  268. pci_add_resource(resources, bus_range);
  269. /* Check for ranges property */
  270. err = of_pci_range_parser_init(&parser, dev_node);
  271. if (err)
  272. return 0;
  273. dev_dbg(dev, "Parsing ranges property...\n");
  274. for_each_of_pci_range(&parser, &range) {
  275. /* Read next ranges element */
  276. if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
  277. range_type = "IO";
  278. else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
  279. range_type = "MEM";
  280. else
  281. range_type = "err";
  282. dev_info(dev, " %6s %#012llx..%#012llx -> %#012llx\n",
  283. range_type, range.cpu_addr,
  284. range.cpu_addr + range.size - 1, range.pci_addr);
  285. /*
  286. * If we failed translation or got a zero-sized region
  287. * then skip this range
  288. */
  289. if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  290. continue;
  291. err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
  292. if (err)
  293. continue;
  294. res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);
  295. if (!res) {
  296. err = -ENOMEM;
  297. goto failed;
  298. }
  299. if (resource_type(res) == IORESOURCE_IO) {
  300. if (!io_base) {
  301. dev_err(dev, "I/O range found for %pOF. Please provide an io_base pointer to save CPU base address\n",
  302. dev_node);
  303. err = -EINVAL;
  304. goto failed;
  305. }
  306. if (*io_base != (resource_size_t)OF_BAD_ADDR)
  307. dev_warn(dev, "More than one I/O resource converted for %pOF. CPU base address for old range lost!\n",
  308. dev_node);
  309. *io_base = range.cpu_addr;
  310. } else if (resource_type(res) == IORESOURCE_MEM) {
  311. res->flags &= ~IORESOURCE_MEM_64;
  312. }
  313. pci_add_resource_offset(resources, res, res->start - range.pci_addr);
  314. }
  315. /* Check for dma-ranges property */
  316. if (!ib_resources)
  317. return 0;
  318. err = of_pci_dma_range_parser_init(&parser, dev_node);
  319. if (err)
  320. return 0;
  321. dev_dbg(dev, "Parsing dma-ranges property...\n");
  322. for_each_of_pci_range(&parser, &range) {
  323. /*
  324. * If we failed translation or got a zero-sized region
  325. * then skip this range
  326. */
  327. if (((range.flags & IORESOURCE_TYPE_BITS) != IORESOURCE_MEM) ||
  328. range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  329. continue;
  330. dev_info(dev, " %6s %#012llx..%#012llx -> %#012llx\n",
  331. "IB MEM", range.cpu_addr,
  332. range.cpu_addr + range.size - 1, range.pci_addr);
  333. err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
  334. if (err)
  335. continue;
  336. res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);
  337. if (!res) {
  338. err = -ENOMEM;
  339. goto failed;
  340. }
  341. pci_add_resource_offset(ib_resources, res,
  342. res->start - range.pci_addr);
  343. }
  344. return 0;
  345. failed:
  346. pci_free_resource_list(resources);
  347. return err;
  348. }
  349. #if IS_ENABLED(CONFIG_OF_IRQ)
  350. /**
  351. * of_irq_parse_pci - Resolve the interrupt for a PCI device
  352. * @pdev: the device whose interrupt is to be resolved
  353. * @out_irq: structure of_phandle_args filled by this function
  354. *
  355. * This function resolves the PCI interrupt for a given PCI device. If a
  356. * device-node exists for a given pci_dev, it will use normal OF tree
  357. * walking. If not, it will implement standard swizzling and walk up the
  358. * PCI tree until an device-node is found, at which point it will finish
  359. * resolving using the OF tree walking.
  360. */
  361. static int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq)
  362. {
  363. struct device_node *dn, *ppnode = NULL;
  364. struct pci_dev *ppdev;
  365. __be32 laddr[3];
  366. u8 pin;
  367. int rc;
  368. /*
  369. * Check if we have a device node, if yes, fallback to standard
  370. * device tree parsing
  371. */
  372. dn = pci_device_to_OF_node(pdev);
  373. if (dn) {
  374. rc = of_irq_parse_one(dn, 0, out_irq);
  375. if (!rc)
  376. return rc;
  377. }
  378. /*
  379. * Ok, we don't, time to have fun. Let's start by building up an
  380. * interrupt spec. we assume #interrupt-cells is 1, which is standard
  381. * for PCI. If you do different, then don't use that routine.
  382. */
  383. rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  384. if (rc != 0)
  385. goto err;
  386. /* No pin, exit with no error message. */
  387. if (pin == 0)
  388. return -ENODEV;
  389. /* Local interrupt-map in the device node? Use it! */
  390. if (of_get_property(dn, "interrupt-map", NULL)) {
  391. pin = pci_swizzle_interrupt_pin(pdev, pin);
  392. ppnode = dn;
  393. }
  394. /* Now we walk up the PCI tree */
  395. while (!ppnode) {
  396. /* Get the pci_dev of our parent */
  397. ppdev = pdev->bus->self;
  398. /* Ouch, it's a host bridge... */
  399. if (ppdev == NULL) {
  400. ppnode = pci_bus_to_OF_node(pdev->bus);
  401. /* No node for host bridge ? give up */
  402. if (ppnode == NULL) {
  403. rc = -EINVAL;
  404. goto err;
  405. }
  406. } else {
  407. /* We found a P2P bridge, check if it has a node */
  408. ppnode = pci_device_to_OF_node(ppdev);
  409. }
  410. /*
  411. * Ok, we have found a parent with a device-node, hand over to
  412. * the OF parsing code.
  413. * We build a unit address from the linux device to be used for
  414. * resolution. Note that we use the linux bus number which may
  415. * not match your firmware bus numbering.
  416. * Fortunately, in most cases, interrupt-map-mask doesn't
  417. * include the bus number as part of the matching.
  418. * You should still be careful about that though if you intend
  419. * to rely on this function (you ship a firmware that doesn't
  420. * create device nodes for all PCI devices).
  421. */
  422. if (ppnode)
  423. break;
  424. /*
  425. * We can only get here if we hit a P2P bridge with no node;
  426. * let's do standard swizzling and try again
  427. */
  428. pin = pci_swizzle_interrupt_pin(pdev, pin);
  429. pdev = ppdev;
  430. }
  431. out_irq->np = ppnode;
  432. out_irq->args_count = 1;
  433. out_irq->args[0] = pin;
  434. laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8));
  435. laddr[1] = laddr[2] = cpu_to_be32(0);
  436. rc = of_irq_parse_raw(laddr, out_irq);
  437. if (rc)
  438. goto err;
  439. return 0;
  440. err:
  441. if (rc == -ENOENT) {
  442. dev_warn(&pdev->dev,
  443. "%s: no interrupt-map found, INTx interrupts not available\n",
  444. __func__);
  445. pr_warn_once("%s: possibly some PCI slots don't have level triggered interrupts capability\n",
  446. __func__);
  447. } else {
  448. dev_err(&pdev->dev, "%s: failed with rc=%d\n", __func__, rc);
  449. }
  450. return rc;
  451. }
  452. /**
  453. * of_irq_parse_and_map_pci() - Decode a PCI IRQ from the device tree and map to a VIRQ
  454. * @dev: The PCI device needing an IRQ
  455. * @slot: PCI slot number; passed when used as map_irq callback. Unused
  456. * @pin: PCI IRQ pin number; passed when used as map_irq callback. Unused
  457. *
  458. * @slot and @pin are unused, but included in the function so that this
  459. * function can be used directly as the map_irq callback to
  460. * pci_assign_irq() and struct pci_host_bridge.map_irq pointer
  461. */
  462. int of_irq_parse_and_map_pci(const struct pci_dev *dev, u8 slot, u8 pin)
  463. {
  464. struct of_phandle_args oirq;
  465. int ret;
  466. ret = of_irq_parse_pci(dev, &oirq);
  467. if (ret)
  468. return 0; /* Proper return code 0 == NO_IRQ */
  469. return irq_create_of_mapping(&oirq);
  470. }
  471. EXPORT_SYMBOL_GPL(of_irq_parse_and_map_pci);
  472. #endif /* CONFIG_OF_IRQ */
  473. static int pci_parse_request_of_pci_ranges(struct device *dev,
  474. struct pci_host_bridge *bridge)
  475. {
  476. int err, res_valid = 0;
  477. resource_size_t iobase;
  478. struct resource_entry *win, *tmp;
  479. INIT_LIST_HEAD(&bridge->windows);
  480. INIT_LIST_HEAD(&bridge->dma_ranges);
  481. err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, &bridge->windows,
  482. &bridge->dma_ranges, &iobase);
  483. if (err)
  484. return err;
  485. err = devm_request_pci_bus_resources(dev, &bridge->windows);
  486. if (err)
  487. return err;
  488. resource_list_for_each_entry_safe(win, tmp, &bridge->windows) {
  489. struct resource *res = win->res;
  490. switch (resource_type(res)) {
  491. case IORESOURCE_IO:
  492. err = devm_pci_remap_iospace(dev, res, iobase);
  493. if (err) {
  494. dev_warn(dev, "error %d: failed to map resource %pR\n",
  495. err, res);
  496. resource_list_destroy_entry(win);
  497. }
  498. break;
  499. case IORESOURCE_MEM:
  500. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  501. if (!(res->flags & IORESOURCE_PREFETCH))
  502. if (upper_32_bits(resource_size(res)))
  503. dev_warn(dev, "Memory resource size exceeds max for 32 bits\n");
  504. break;
  505. }
  506. }
  507. if (!res_valid)
  508. dev_warn(dev, "non-prefetchable memory resource required\n");
  509. return 0;
  510. }
  511. int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge)
  512. {
  513. if (!dev->of_node)
  514. return 0;
  515. bridge->swizzle_irq = pci_common_swizzle;
  516. bridge->map_irq = of_irq_parse_and_map_pci;
  517. return pci_parse_request_of_pci_ranges(dev, bridge);
  518. }
  519. #endif /* CONFIG_PCI */
  520. /**
  521. * of_pci_get_max_link_speed - Find the maximum link speed of the given device node.
  522. * @node: Device tree node with the maximum link speed information.
  523. *
  524. * This function will try to find the limitation of link speed by finding
  525. * a property called "max-link-speed" of the given device node.
  526. *
  527. * Return:
  528. * * > 0 - On success, a maximum link speed.
  529. * * -EINVAL - Invalid "max-link-speed" property value, or failure to access
  530. * the property of the device tree node.
  531. *
  532. * Returns the associated max link speed from DT, or a negative value if the
  533. * required property is not found or is invalid.
  534. */
  535. int of_pci_get_max_link_speed(struct device_node *node)
  536. {
  537. u32 max_link_speed;
  538. if (of_property_read_u32(node, "max-link-speed", &max_link_speed) ||
  539. max_link_speed == 0 || max_link_speed > 4)
  540. return -EINVAL;
  541. return max_link_speed;
  542. }
  543. EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed);
  544. /**
  545. * of_pci_get_slot_power_limit - Parses the "slot-power-limit-milliwatt"
  546. * property.
  547. *
  548. * @node: device tree node with the slot power limit information
  549. * @slot_power_limit_value: pointer where the value should be stored in PCIe
  550. * Slot Capabilities Register format
  551. * @slot_power_limit_scale: pointer where the scale should be stored in PCIe
  552. * Slot Capabilities Register format
  553. *
  554. * Returns the slot power limit in milliwatts and if @slot_power_limit_value
  555. * and @slot_power_limit_scale pointers are non-NULL, fills in the value and
  556. * scale in format used by PCIe Slot Capabilities Register.
  557. *
  558. * If the property is not found or is invalid, returns 0.
  559. */
  560. u32 of_pci_get_slot_power_limit(struct device_node *node,
  561. u8 *slot_power_limit_value,
  562. u8 *slot_power_limit_scale)
  563. {
  564. u32 slot_power_limit_mw;
  565. u8 value, scale;
  566. if (of_property_read_u32(node, "slot-power-limit-milliwatt",
  567. &slot_power_limit_mw))
  568. slot_power_limit_mw = 0;
  569. /* Calculate Slot Power Limit Value and Slot Power Limit Scale */
  570. if (slot_power_limit_mw == 0) {
  571. value = 0x00;
  572. scale = 0;
  573. } else if (slot_power_limit_mw <= 255) {
  574. value = slot_power_limit_mw;
  575. scale = 3;
  576. } else if (slot_power_limit_mw <= 255*10) {
  577. value = slot_power_limit_mw / 10;
  578. scale = 2;
  579. slot_power_limit_mw = slot_power_limit_mw / 10 * 10;
  580. } else if (slot_power_limit_mw <= 255*100) {
  581. value = slot_power_limit_mw / 100;
  582. scale = 1;
  583. slot_power_limit_mw = slot_power_limit_mw / 100 * 100;
  584. } else if (slot_power_limit_mw <= 239*1000) {
  585. value = slot_power_limit_mw / 1000;
  586. scale = 0;
  587. slot_power_limit_mw = slot_power_limit_mw / 1000 * 1000;
  588. } else if (slot_power_limit_mw < 250*1000) {
  589. value = 0xEF;
  590. scale = 0;
  591. slot_power_limit_mw = 239*1000;
  592. } else if (slot_power_limit_mw <= 600*1000) {
  593. value = 0xF0 + (slot_power_limit_mw / 1000 - 250) / 25;
  594. scale = 0;
  595. slot_power_limit_mw = slot_power_limit_mw / (1000*25) * (1000*25);
  596. } else {
  597. value = 0xFE;
  598. scale = 0;
  599. slot_power_limit_mw = 600*1000;
  600. }
  601. if (slot_power_limit_value)
  602. *slot_power_limit_value = value;
  603. if (slot_power_limit_scale)
  604. *slot_power_limit_scale = scale;
  605. return slot_power_limit_mw;
  606. }
  607. EXPORT_SYMBOL_GPL(of_pci_get_slot_power_limit);