pci.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1997, 1998 Ralf Baechle
  7. * Copyright (C) 1999 SuSE GmbH
  8. * Copyright (C) 1999-2001 Hewlett-Packard Company
  9. * Copyright (C) 1999-2001 Grant Grundler
  10. */
  11. #include <linux/eisa.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/pci.h>
  16. #include <linux/types.h>
  17. #include <asm/io.h>
  18. #include <asm/system.h>
  19. #include <asm/superio.h>
  20. #define DEBUG_RESOURCES 0
  21. #define DEBUG_CONFIG 0
  22. #if DEBUG_CONFIG
  23. # define DBGC(x...) printk(KERN_DEBUG x)
  24. #else
  25. # define DBGC(x...)
  26. #endif
  27. #if DEBUG_RESOURCES
  28. #define DBG_RES(x...) printk(KERN_DEBUG x)
  29. #else
  30. #define DBG_RES(x...)
  31. #endif
  32. /* To be used as: mdelay(pci_post_reset_delay);
  33. *
  34. * post_reset is the time the kernel should stall to prevent anyone from
  35. * accessing the PCI bus once #RESET is de-asserted.
  36. * PCI spec somewhere says 1 second but with multi-PCI bus systems,
  37. * this makes the boot time much longer than necessary.
  38. * 20ms seems to work for all the HP PCI implementations to date.
  39. *
  40. * #define pci_post_reset_delay 50
  41. */
  42. struct pci_port_ops *pci_port __read_mostly;
  43. struct pci_bios_ops *pci_bios __read_mostly;
  44. static int pci_hba_count __read_mostly;
  45. /* parisc_pci_hba used by pci_port->in/out() ops to lookup bus data. */
  46. #define PCI_HBA_MAX 32
  47. static struct pci_hba_data *parisc_pci_hba[PCI_HBA_MAX] __read_mostly;
  48. /********************************************************************
  49. **
  50. ** I/O port space support
  51. **
  52. *********************************************************************/
  53. /* EISA port numbers and PCI port numbers share the same interface. Some
  54. * machines have both EISA and PCI adapters installed. Rather than turn
  55. * pci_port into an array, we reserve bus 0 for EISA and call the EISA
  56. * routines if the access is to a port on bus 0. We don't want to fix
  57. * EISA and ISA drivers which assume port space is <= 0xffff.
  58. */
  59. #ifdef CONFIG_EISA
  60. #define EISA_IN(size) if (EISA_bus && (b == 0)) return eisa_in##size(addr)
  61. #define EISA_OUT(size) if (EISA_bus && (b == 0)) return eisa_out##size(d, addr)
  62. #else
  63. #define EISA_IN(size)
  64. #define EISA_OUT(size)
  65. #endif
  66. #define PCI_PORT_IN(type, size) \
  67. u##size in##type (int addr) \
  68. { \
  69. int b = PCI_PORT_HBA(addr); \
  70. EISA_IN(size); \
  71. if (!parisc_pci_hba[b]) return (u##size) -1; \
  72. return pci_port->in##type(parisc_pci_hba[b], PCI_PORT_ADDR(addr)); \
  73. } \
  74. EXPORT_SYMBOL(in##type);
  75. PCI_PORT_IN(b, 8)
  76. PCI_PORT_IN(w, 16)
  77. PCI_PORT_IN(l, 32)
  78. #define PCI_PORT_OUT(type, size) \
  79. void out##type (u##size d, int addr) \
  80. { \
  81. int b = PCI_PORT_HBA(addr); \
  82. EISA_OUT(size); \
  83. if (!parisc_pci_hba[b]) return; \
  84. pci_port->out##type(parisc_pci_hba[b], PCI_PORT_ADDR(addr), d); \
  85. } \
  86. EXPORT_SYMBOL(out##type);
  87. PCI_PORT_OUT(b, 8)
  88. PCI_PORT_OUT(w, 16)
  89. PCI_PORT_OUT(l, 32)
  90. /*
  91. * BIOS32 replacement.
  92. */
  93. static int __init pcibios_init(void)
  94. {
  95. if (!pci_bios)
  96. return -1;
  97. if (pci_bios->init) {
  98. pci_bios->init();
  99. } else {
  100. printk(KERN_WARNING "pci_bios != NULL but init() is!\n");
  101. }
  102. /* Set the CLS for PCI as early as possible. */
  103. pci_cache_line_size = pci_dfl_cache_line_size;
  104. return 0;
  105. }
  106. /* Called from pci_do_scan_bus() *after* walking a bus but before walking PPBs. */
  107. void pcibios_fixup_bus(struct pci_bus *bus)
  108. {
  109. if (pci_bios->fixup_bus) {
  110. pci_bios->fixup_bus(bus);
  111. } else {
  112. printk(KERN_WARNING "pci_bios != NULL but fixup_bus() is!\n");
  113. }
  114. }
  115. char *pcibios_setup(char *str)
  116. {
  117. return str;
  118. }
  119. /*
  120. * Called by pci_set_master() - a driver interface.
  121. *
  122. * Legacy PDC guarantees to set:
  123. * Map Memory BAR's into PA IO space.
  124. * Map Expansion ROM BAR into one common PA IO space per bus.
  125. * Map IO BAR's into PCI IO space.
  126. * Command (see below)
  127. * Cache Line Size
  128. * Latency Timer
  129. * Interrupt Line
  130. * PPB: secondary latency timer, io/mmio base/limit,
  131. * bus numbers, bridge control
  132. *
  133. */
  134. void pcibios_set_master(struct pci_dev *dev)
  135. {
  136. u8 lat;
  137. /* If someone already mucked with this, don't touch it. */
  138. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  139. if (lat >= 16) return;
  140. /*
  141. ** HP generally has fewer devices on the bus than other architectures.
  142. ** upper byte is PCI_LATENCY_TIMER.
  143. */
  144. pci_write_config_word(dev, PCI_CACHE_LINE_SIZE,
  145. (0x80 << 8) | pci_cache_line_size);
  146. }
  147. void __init pcibios_init_bus(struct pci_bus *bus)
  148. {
  149. struct pci_dev *dev = bus->self;
  150. unsigned short bridge_ctl;
  151. /* We deal only with pci controllers and pci-pci bridges. */
  152. if (!dev || (dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  153. return;
  154. /* PCI-PCI bridge - set the cache line and default latency
  155. (32) for primary and secondary buses. */
  156. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, 32);
  157. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bridge_ctl);
  158. bridge_ctl |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR;
  159. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bridge_ctl);
  160. }
  161. /* called by drivers/pci/setup-bus.c:pci_setup_bridge(). */
  162. void __devinit pcibios_resource_to_bus(struct pci_dev *dev,
  163. struct pci_bus_region *region, struct resource *res)
  164. {
  165. #ifdef CONFIG_64BIT
  166. struct pci_hba_data *hba = HBA_DATA(dev->bus->bridge->platform_data);
  167. #endif
  168. if (res->flags & IORESOURCE_IO) {
  169. /*
  170. ** I/O space may see busnumbers here. Something
  171. ** in the form of 0xbbxxxx where bb is the bus num
  172. ** and xxxx is the I/O port space address.
  173. ** Remaining address translation are done in the
  174. ** PCI Host adapter specific code - ie dino_out8.
  175. */
  176. region->start = PCI_PORT_ADDR(res->start);
  177. region->end = PCI_PORT_ADDR(res->end);
  178. } else if (res->flags & IORESOURCE_MEM) {
  179. /* Convert MMIO addr to PCI addr (undo global virtualization) */
  180. region->start = PCI_BUS_ADDR(hba, res->start);
  181. region->end = PCI_BUS_ADDR(hba, res->end);
  182. }
  183. DBG_RES("pcibios_resource_to_bus(%02x %s [%lx,%lx])\n",
  184. dev->bus->number, res->flags & IORESOURCE_IO ? "IO" : "MEM",
  185. region->start, region->end);
  186. }
  187. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  188. struct pci_bus_region *region)
  189. {
  190. #ifdef CONFIG_64BIT
  191. struct pci_hba_data *hba = HBA_DATA(dev->bus->bridge->platform_data);
  192. #endif
  193. if (res->flags & IORESOURCE_MEM) {
  194. res->start = PCI_HOST_ADDR(hba, region->start);
  195. res->end = PCI_HOST_ADDR(hba, region->end);
  196. }
  197. if (res->flags & IORESOURCE_IO) {
  198. res->start = region->start;
  199. res->end = region->end;
  200. }
  201. }
  202. #ifdef CONFIG_HOTPLUG
  203. EXPORT_SYMBOL(pcibios_resource_to_bus);
  204. EXPORT_SYMBOL(pcibios_bus_to_resource);
  205. #endif
  206. /*
  207. * pcibios align resources() is called every time generic PCI code
  208. * wants to generate a new address. The process of looking for
  209. * an available address, each candidate is first "aligned" and
  210. * then checked if the resource is available until a match is found.
  211. *
  212. * Since we are just checking candidates, don't use any fields other
  213. * than res->start.
  214. */
  215. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  216. resource_size_t size, resource_size_t alignment)
  217. {
  218. resource_size_t mask, align, start = res->start;
  219. DBG_RES("pcibios_align_resource(%s, (%p) [%lx,%lx]/%x, 0x%lx, 0x%lx)\n",
  220. pci_name(((struct pci_dev *) data)),
  221. res->parent, res->start, res->end,
  222. (int) res->flags, size, alignment);
  223. /* If it's not IO, then it's gotta be MEM */
  224. align = (res->flags & IORESOURCE_IO) ? PCIBIOS_MIN_IO : PCIBIOS_MIN_MEM;
  225. /* Align to largest of MIN or input size */
  226. mask = max(alignment, align) - 1;
  227. start += mask;
  228. start &= ~mask;
  229. return start;
  230. }
  231. /*
  232. * A driver is enabling the device. We make sure that all the appropriate
  233. * bits are set to allow the device to operate as the driver is expecting.
  234. * We enable the port IO and memory IO bits if the device has any BARs of
  235. * that type, and we enable the PERR and SERR bits unconditionally.
  236. * Drivers that do not need parity (eg graphics and possibly networking)
  237. * can clear these bits if they want.
  238. */
  239. int pcibios_enable_device(struct pci_dev *dev, int mask)
  240. {
  241. int err;
  242. u16 cmd, old_cmd;
  243. err = pci_enable_resources(dev, mask);
  244. if (err < 0)
  245. return err;
  246. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  247. old_cmd = cmd;
  248. cmd |= (PCI_COMMAND_SERR | PCI_COMMAND_PARITY);
  249. #if 0
  250. /* If bridge/bus controller has FBB enabled, child must too. */
  251. if (dev->bus->bridge_ctl & PCI_BRIDGE_CTL_FAST_BACK)
  252. cmd |= PCI_COMMAND_FAST_BACK;
  253. #endif
  254. if (cmd != old_cmd) {
  255. dev_info(&dev->dev, "enabling SERR and PARITY (%04x -> %04x)\n",
  256. old_cmd, cmd);
  257. pci_write_config_word(dev, PCI_COMMAND, cmd);
  258. }
  259. return 0;
  260. }
  261. /* PA-RISC specific */
  262. void pcibios_register_hba(struct pci_hba_data *hba)
  263. {
  264. if (pci_hba_count >= PCI_HBA_MAX) {
  265. printk(KERN_ERR "PCI: Too many Host Bus Adapters\n");
  266. return;
  267. }
  268. parisc_pci_hba[pci_hba_count] = hba;
  269. hba->hba_num = pci_hba_count++;
  270. }
  271. subsys_initcall(pcibios_init);