pci.c 9.7 KB

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