pci_of_scan.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Helper routines to scan the device tree for PCI devices and busses
  3. *
  4. * Migrated out of PowerPC architecture pci_64.c file by Grant Likely
  5. * <grant.likely@secretlab.ca> so that these routines are available for
  6. * 32 bit also.
  7. *
  8. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  9. * Rework, based on alpha PCI code.
  10. * Copyright (c) 2009 Secret Lab Technologies Ltd.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * version 2 as published by the Free Software Foundation.
  15. */
  16. #include <linux/pci.h>
  17. #include <linux/export.h>
  18. #include <asm/pci-bridge.h>
  19. #include <asm/prom.h>
  20. /**
  21. * get_int_prop - Decode a u32 from a device tree property
  22. */
  23. static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
  24. {
  25. const u32 *prop;
  26. int len;
  27. prop = of_get_property(np, name, &len);
  28. if (prop && len >= 4)
  29. return *prop;
  30. return def;
  31. }
  32. /**
  33. * pci_parse_of_flags - Parse the flags cell of a device tree PCI address
  34. * @addr0: value of 1st cell of a device tree PCI address.
  35. * @bridge: Set this flag if the address is from a bridge 'ranges' property
  36. */
  37. unsigned int pci_parse_of_flags(u32 addr0, int bridge)
  38. {
  39. unsigned int flags = 0;
  40. if (addr0 & 0x02000000) {
  41. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  42. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  43. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  44. if (addr0 & 0x40000000)
  45. flags |= IORESOURCE_PREFETCH
  46. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  47. /* Note: We don't know whether the ROM has been left enabled
  48. * by the firmware or not. We mark it as disabled (ie, we do
  49. * not set the IORESOURCE_ROM_ENABLE flag) for now rather than
  50. * do a config space read, it will be force-enabled if needed
  51. */
  52. if (!bridge && (addr0 & 0xff) == 0x30)
  53. flags |= IORESOURCE_READONLY;
  54. } else if (addr0 & 0x01000000)
  55. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  56. if (flags)
  57. flags |= IORESOURCE_SIZEALIGN;
  58. return flags;
  59. }
  60. /**
  61. * of_pci_parse_addrs - Parse PCI addresses assigned in the device tree node
  62. * @node: device tree node for the PCI device
  63. * @dev: pci_dev structure for the device
  64. *
  65. * This function parses the 'assigned-addresses' property of a PCI devices'
  66. * device tree node and writes them into the associated pci_dev structure.
  67. */
  68. static void of_pci_parse_addrs(struct device_node *node, struct pci_dev *dev)
  69. {
  70. u64 base, size;
  71. unsigned int flags;
  72. struct pci_bus_region region;
  73. struct resource *res;
  74. const u32 *addrs;
  75. u32 i;
  76. int proplen;
  77. addrs = of_get_property(node, "assigned-addresses", &proplen);
  78. if (!addrs)
  79. return;
  80. pr_debug(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
  81. for (; proplen >= 20; proplen -= 20, addrs += 5) {
  82. flags = pci_parse_of_flags(addrs[0], 0);
  83. if (!flags)
  84. continue;
  85. base = of_read_number(&addrs[1], 2);
  86. size = of_read_number(&addrs[3], 2);
  87. if (!size)
  88. continue;
  89. i = addrs[0] & 0xff;
  90. pr_debug(" base: %llx, size: %llx, i: %x\n",
  91. (unsigned long long)base,
  92. (unsigned long long)size, i);
  93. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  94. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  95. } else if (i == dev->rom_base_reg) {
  96. res = &dev->resource[PCI_ROM_RESOURCE];
  97. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  98. } else {
  99. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  100. continue;
  101. }
  102. res->flags = flags;
  103. res->name = pci_name(dev);
  104. region.start = base;
  105. region.end = base + size - 1;
  106. pcibios_bus_to_resource(dev, res, &region);
  107. }
  108. }
  109. /**
  110. * of_create_pci_dev - Given a device tree node on a pci bus, create a pci_dev
  111. * @node: device tree node pointer
  112. * @bus: bus the device is sitting on
  113. * @devfn: PCI function number, extracted from device tree by caller.
  114. */
  115. struct pci_dev *of_create_pci_dev(struct device_node *node,
  116. struct pci_bus *bus, int devfn)
  117. {
  118. struct pci_dev *dev;
  119. const char *type;
  120. struct pci_slot *slot;
  121. dev = pci_alloc_dev(bus);
  122. if (!dev)
  123. return NULL;
  124. type = of_get_property(node, "device_type", NULL);
  125. if (type == NULL)
  126. type = "";
  127. pr_debug(" create device, devfn: %x, type: %s\n", devfn, type);
  128. dev->dev.of_node = of_node_get(node);
  129. dev->dev.parent = bus->bridge;
  130. dev->dev.bus = &pci_bus_type;
  131. dev->devfn = devfn;
  132. dev->multifunction = 0; /* maybe a lie? */
  133. dev->needs_freset = 0; /* pcie fundamental reset required */
  134. set_pcie_port_type(dev);
  135. list_for_each_entry(slot, &dev->bus->slots, list)
  136. if (PCI_SLOT(dev->devfn) == slot->number)
  137. dev->slot = slot;
  138. dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
  139. dev->device = get_int_prop(node, "device-id", 0xffff);
  140. dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
  141. dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
  142. dev->cfg_size = pci_cfg_space_size(dev);
  143. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  144. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  145. dev->class = get_int_prop(node, "class-code", 0);
  146. dev->revision = get_int_prop(node, "revision-id", 0);
  147. pr_debug(" class: 0x%x\n", dev->class);
  148. pr_debug(" revision: 0x%x\n", dev->revision);
  149. dev->current_state = PCI_UNKNOWN; /* unknown power state */
  150. dev->error_state = pci_channel_io_normal;
  151. dev->dma_mask = 0xffffffff;
  152. /* Early fixups, before probing the BARs */
  153. pci_fixup_device(pci_fixup_early, dev);
  154. if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
  155. /* a PCI-PCI bridge */
  156. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  157. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  158. set_pcie_hotplug_bridge(dev);
  159. } else if (!strcmp(type, "cardbus")) {
  160. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  161. } else {
  162. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  163. dev->rom_base_reg = PCI_ROM_ADDRESS;
  164. /* Maybe do a default OF mapping here */
  165. dev->irq = NO_IRQ;
  166. }
  167. of_pci_parse_addrs(node, dev);
  168. pr_debug(" adding to system ...\n");
  169. pci_device_add(dev, bus);
  170. return dev;
  171. }
  172. EXPORT_SYMBOL(of_create_pci_dev);
  173. /**
  174. * of_scan_pci_bridge - Set up a PCI bridge and scan for child nodes
  175. * @dev: pci_dev structure for the bridge
  176. *
  177. * of_scan_bus() calls this routine for each PCI bridge that it finds, and
  178. * this routine in turn call of_scan_bus() recusively to scan for more child
  179. * devices.
  180. */
  181. void of_scan_pci_bridge(struct pci_dev *dev)
  182. {
  183. struct device_node *node = dev->dev.of_node;
  184. struct pci_bus *bus;
  185. const u32 *busrange, *ranges;
  186. int len, i, mode;
  187. struct pci_bus_region region;
  188. struct resource *res;
  189. unsigned int flags;
  190. u64 size;
  191. pr_debug("of_scan_pci_bridge(%s)\n", node->full_name);
  192. /* parse bus-range property */
  193. busrange = of_get_property(node, "bus-range", &len);
  194. if (busrange == NULL || len != 8) {
  195. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  196. node->full_name);
  197. return;
  198. }
  199. ranges = of_get_property(node, "ranges", &len);
  200. if (ranges == NULL) {
  201. printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
  202. node->full_name);
  203. return;
  204. }
  205. bus = pci_find_bus(pci_domain_nr(dev->bus), busrange[0]);
  206. if (!bus) {
  207. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  208. if (!bus) {
  209. printk(KERN_ERR "Failed to create pci bus for %s\n",
  210. node->full_name);
  211. return;
  212. }
  213. }
  214. bus->primary = dev->bus->number;
  215. pci_bus_insert_busn_res(bus, busrange[0], busrange[1]);
  216. bus->bridge_ctl = 0;
  217. /* parse ranges property */
  218. /* PCI #address-cells == 3 and #size-cells == 2 always */
  219. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  220. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  221. res->flags = 0;
  222. bus->resource[i] = res;
  223. ++res;
  224. }
  225. i = 1;
  226. for (; len >= 32; len -= 32, ranges += 8) {
  227. flags = pci_parse_of_flags(ranges[0], 1);
  228. size = of_read_number(&ranges[6], 2);
  229. if (flags == 0 || size == 0)
  230. continue;
  231. if (flags & IORESOURCE_IO) {
  232. res = bus->resource[0];
  233. if (res->flags) {
  234. printk(KERN_ERR "PCI: ignoring extra I/O range"
  235. " for bridge %s\n", node->full_name);
  236. continue;
  237. }
  238. } else {
  239. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  240. printk(KERN_ERR "PCI: too many memory ranges"
  241. " for bridge %s\n", node->full_name);
  242. continue;
  243. }
  244. res = bus->resource[i];
  245. ++i;
  246. }
  247. res->flags = flags;
  248. region.start = of_read_number(&ranges[1], 2);
  249. region.end = region.start + size - 1;
  250. pcibios_bus_to_resource(dev, res, &region);
  251. }
  252. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  253. bus->number);
  254. pr_debug(" bus name: %s\n", bus->name);
  255. mode = PCI_PROBE_NORMAL;
  256. if (ppc_md.pci_probe_mode)
  257. mode = ppc_md.pci_probe_mode(bus);
  258. pr_debug(" probe mode: %d\n", mode);
  259. if (mode == PCI_PROBE_DEVTREE)
  260. of_scan_bus(node, bus);
  261. else if (mode == PCI_PROBE_NORMAL)
  262. pci_scan_child_bus(bus);
  263. }
  264. EXPORT_SYMBOL(of_scan_pci_bridge);
  265. static struct pci_dev *of_scan_pci_dev(struct pci_bus *bus,
  266. struct device_node *dn)
  267. {
  268. struct pci_dev *dev = NULL;
  269. const u32 *reg;
  270. int reglen, devfn;
  271. pr_debug(" * %s\n", dn->full_name);
  272. if (!of_device_is_available(dn))
  273. return NULL;
  274. reg = of_get_property(dn, "reg", &reglen);
  275. if (reg == NULL || reglen < 20)
  276. return NULL;
  277. devfn = (reg[0] >> 8) & 0xff;
  278. /* Check if the PCI device is already there */
  279. dev = pci_get_slot(bus, devfn);
  280. if (dev) {
  281. pci_dev_put(dev);
  282. return dev;
  283. }
  284. /* create a new pci_dev for this device */
  285. dev = of_create_pci_dev(dn, bus, devfn);
  286. if (!dev)
  287. return NULL;
  288. pr_debug(" dev header type: %x\n", dev->hdr_type);
  289. return dev;
  290. }
  291. /**
  292. * __of_scan_bus - given a PCI bus node, setup bus and scan for child devices
  293. * @node: device tree node for the PCI bus
  294. * @bus: pci_bus structure for the PCI bus
  295. * @rescan_existing: Flag indicating bus has already been set up
  296. */
  297. static void __of_scan_bus(struct device_node *node, struct pci_bus *bus,
  298. int rescan_existing)
  299. {
  300. struct device_node *child;
  301. struct pci_dev *dev;
  302. pr_debug("of_scan_bus(%s) bus no %d...\n",
  303. node->full_name, bus->number);
  304. /* Scan direct children */
  305. for_each_child_of_node(node, child) {
  306. dev = of_scan_pci_dev(bus, child);
  307. if (!dev)
  308. continue;
  309. pr_debug(" dev header type: %x\n", dev->hdr_type);
  310. }
  311. /* Apply all fixups necessary. We don't fixup the bus "self"
  312. * for an existing bridge that is being rescanned
  313. */
  314. if (!rescan_existing)
  315. pcibios_setup_bus_self(bus);
  316. pcibios_setup_bus_devices(bus);
  317. /* Now scan child busses */
  318. list_for_each_entry(dev, &bus->devices, bus_list) {
  319. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  320. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  321. of_scan_pci_bridge(dev);
  322. }
  323. }
  324. }
  325. /**
  326. * of_scan_bus - given a PCI bus node, setup bus and scan for child devices
  327. * @node: device tree node for the PCI bus
  328. * @bus: pci_bus structure for the PCI bus
  329. */
  330. void of_scan_bus(struct device_node *node, struct pci_bus *bus)
  331. {
  332. __of_scan_bus(node, bus, 0);
  333. }
  334. EXPORT_SYMBOL_GPL(of_scan_bus);
  335. /**
  336. * of_rescan_bus - given a PCI bus node, scan for child devices
  337. * @node: device tree node for the PCI bus
  338. * @bus: pci_bus structure for the PCI bus
  339. *
  340. * Same as of_scan_bus, but for a pci_bus structure that has already been
  341. * setup.
  342. */
  343. void of_rescan_bus(struct device_node *node, struct pci_bus *bus)
  344. {
  345. __of_scan_bus(node, bus, 1);
  346. }
  347. EXPORT_SYMBOL_GPL(of_rescan_bus);