pci_of_scan.c 10 KB

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