pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * arch/xtensa/pcibios.c
  3. *
  4. * PCI bios-type initialisation for PCI machines
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * Copyright (C) 2001-2005 Tensilica Inc.
  12. *
  13. * Based largely on work from Cort (ppc/kernel/pci.c)
  14. * IO functions copied from sparc.
  15. *
  16. * Chris Zankel <chris@zankel.net>
  17. *
  18. */
  19. #include <linux/config.h>
  20. #include <linux/kernel.h>
  21. #include <linux/pci.h>
  22. #include <linux/delay.h>
  23. #include <linux/string.h>
  24. #include <linux/init.h>
  25. #include <linux/sched.h>
  26. #include <linux/errno.h>
  27. #include <linux/bootmem.h>
  28. #include <asm/pci-bridge.h>
  29. #include <asm/platform.h>
  30. #undef DEBUG
  31. #ifdef DEBUG
  32. #define DBG(x...) printk(x)
  33. #else
  34. #define DBG(x...)
  35. #endif
  36. /* PCI Controller */
  37. /*
  38. * pcibios_alloc_controller
  39. * pcibios_enable_device
  40. * pcibios_fixups
  41. * pcibios_align_resource
  42. * pcibios_fixup_bus
  43. * pcibios_setup
  44. * pci_bus_add_device
  45. * pci_mmap_page_range
  46. */
  47. struct pci_controller* pci_ctrl_head;
  48. struct pci_controller** pci_ctrl_tail = &pci_ctrl_head;
  49. static int pci_bus_count;
  50. /*
  51. * We need to avoid collisions with `mirrored' VGA ports
  52. * and other strange ISA hardware, so we always want the
  53. * addresses to be allocated in the 0x000-0x0ff region
  54. * modulo 0x400.
  55. *
  56. * Why? Because some silly external IO cards only decode
  57. * the low 10 bits of the IO address. The 0x00-0xff region
  58. * is reserved for motherboard devices that decode all 16
  59. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  60. * but we want to try to avoid allocating at 0x2900-0x2bff
  61. * which might have be mirrored at 0x0100-0x03ff..
  62. */
  63. void
  64. pcibios_align_resource(void *data, struct resource *res, unsigned long size,
  65. unsigned long align)
  66. {
  67. struct pci_dev *dev = data;
  68. if (res->flags & IORESOURCE_IO) {
  69. unsigned long start = res->start;
  70. if (size > 0x100) {
  71. printk(KERN_ERR "PCI: I/O Region %s/%d too large"
  72. " (%ld bytes)\n", pci_name(dev),
  73. dev->resource - res, size);
  74. }
  75. if (start & 0x300) {
  76. start = (start + 0x3ff) & ~0x3ff;
  77. res->start = start;
  78. }
  79. }
  80. }
  81. int
  82. pcibios_enable_resources(struct pci_dev *dev, int mask)
  83. {
  84. u16 cmd, old_cmd;
  85. int idx;
  86. struct resource *r;
  87. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  88. old_cmd = cmd;
  89. for(idx=0; idx<6; idx++) {
  90. r = &dev->resource[idx];
  91. if (!r->start && r->end) {
  92. printk (KERN_ERR "PCI: Device %s not available because "
  93. "of resource collisions\n", pci_name(dev));
  94. return -EINVAL;
  95. }
  96. if (r->flags & IORESOURCE_IO)
  97. cmd |= PCI_COMMAND_IO;
  98. if (r->flags & IORESOURCE_MEM)
  99. cmd |= PCI_COMMAND_MEMORY;
  100. }
  101. if (dev->resource[PCI_ROM_RESOURCE].start)
  102. cmd |= PCI_COMMAND_MEMORY;
  103. if (cmd != old_cmd) {
  104. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  105. pci_name(dev), old_cmd, cmd);
  106. pci_write_config_word(dev, PCI_COMMAND, cmd);
  107. }
  108. return 0;
  109. }
  110. struct pci_controller * __init pcibios_alloc_controller(void)
  111. {
  112. struct pci_controller *pci_ctrl;
  113. pci_ctrl = (struct pci_controller *)alloc_bootmem(sizeof(*pci_ctrl));
  114. memset(pci_ctrl, 0, sizeof(struct pci_controller));
  115. *pci_ctrl_tail = pci_ctrl;
  116. pci_ctrl_tail = &pci_ctrl->next;
  117. return pci_ctrl;
  118. }
  119. static int __init pcibios_init(void)
  120. {
  121. struct pci_controller *pci_ctrl;
  122. struct pci_bus *bus;
  123. int next_busno = 0, i;
  124. printk("PCI: Probing PCI hardware\n");
  125. /* Scan all of the recorded PCI controllers. */
  126. for (pci_ctrl = pci_ctrl_head; pci_ctrl; pci_ctrl = pci_ctrl->next) {
  127. pci_ctrl->last_busno = 0xff;
  128. bus = pci_scan_bus(pci_ctrl->first_busno, pci_ctrl->ops,
  129. pci_ctrl);
  130. if (pci_ctrl->io_resource.flags) {
  131. unsigned long offs;
  132. offs = (unsigned long)pci_ctrl->io_space.base;
  133. pci_ctrl->io_resource.start += offs;
  134. pci_ctrl->io_resource.end += offs;
  135. bus->resource[0] = &pci_ctrl->io_resource;
  136. }
  137. for (i = 0; i < 3; ++i)
  138. if (pci_ctrl->mem_resources[i].flags)
  139. bus->resource[i+1] =&pci_ctrl->mem_resources[i];
  140. pci_ctrl->bus = bus;
  141. pci_ctrl->last_busno = bus->subordinate;
  142. if (next_busno <= pci_ctrl->last_busno)
  143. next_busno = pci_ctrl->last_busno+1;
  144. }
  145. pci_bus_count = next_busno;
  146. return platform_pcibios_fixup();
  147. }
  148. subsys_initcall(pcibios_init);
  149. void __init pcibios_fixup_bus(struct pci_bus *bus)
  150. {
  151. struct pci_controller *pci_ctrl = bus->sysdata;
  152. struct resource *res;
  153. unsigned long io_offset;
  154. int i;
  155. io_offset = (unsigned long)pci_ctrl->io_space.base;
  156. if (bus->parent == NULL) {
  157. /* this is a host bridge - fill in its resources */
  158. pci_ctrl->bus = bus;
  159. bus->resource[0] = res = &pci_ctrl->io_resource;
  160. if (!res->flags) {
  161. if (io_offset)
  162. printk (KERN_ERR "I/O resource not set for host"
  163. " bridge %d\n", pci_ctrl->index);
  164. res->start = 0;
  165. res->end = IO_SPACE_LIMIT;
  166. res->flags = IORESOURCE_IO;
  167. }
  168. res->start += io_offset;
  169. res->end += io_offset;
  170. for (i = 0; i < 3; i++) {
  171. res = &pci_ctrl->mem_resources[i];
  172. if (!res->flags) {
  173. if (i > 0)
  174. continue;
  175. printk(KERN_ERR "Memory resource not set for "
  176. "host bridge %d\n", pci_ctrl->index);
  177. res->start = 0;
  178. res->end = ~0U;
  179. res->flags = IORESOURCE_MEM;
  180. }
  181. bus->resource[i+1] = res;
  182. }
  183. } else {
  184. /* This is a subordinate bridge */
  185. pci_read_bridge_bases(bus);
  186. for (i = 0; i < 4; i++) {
  187. if ((res = bus->resource[i]) == NULL || !res->flags)
  188. continue;
  189. if (io_offset && (res->flags & IORESOURCE_IO)) {
  190. res->start += io_offset;
  191. res->end += io_offset;
  192. }
  193. }
  194. }
  195. }
  196. char __init *pcibios_setup(char *str)
  197. {
  198. return str;
  199. }
  200. /* the next one is stolen from the alpha port... */
  201. void __init
  202. pcibios_update_irq(struct pci_dev *dev, int irq)
  203. {
  204. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  205. }
  206. int pcibios_enable_device(struct pci_dev *dev, int mask)
  207. {
  208. u16 cmd, old_cmd;
  209. int idx;
  210. struct resource *r;
  211. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  212. old_cmd = cmd;
  213. for (idx=0; idx<6; idx++) {
  214. r = &dev->resource[idx];
  215. if (!r->start && r->end) {
  216. printk(KERN_ERR "PCI: Device %s not available because "
  217. "of resource collisions\n", pci_name(dev));
  218. return -EINVAL;
  219. }
  220. if (r->flags & IORESOURCE_IO)
  221. cmd |= PCI_COMMAND_IO;
  222. if (r->flags & IORESOURCE_MEM)
  223. cmd |= PCI_COMMAND_MEMORY;
  224. }
  225. if (cmd != old_cmd) {
  226. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  227. pci_name(dev), old_cmd, cmd);
  228. pci_write_config_word(dev, PCI_COMMAND, cmd);
  229. }
  230. return 0;
  231. }
  232. #ifdef CONFIG_PROC_FS
  233. /*
  234. * Return the index of the PCI controller for device pdev.
  235. */
  236. int
  237. pci_controller_num(struct pci_dev *dev)
  238. {
  239. struct pci_controller *pci_ctrl = (struct pci_controller*) dev->sysdata;
  240. return pci_ctrl->index;
  241. }
  242. #endif /* CONFIG_PROC_FS */
  243. /*
  244. * Platform support for /proc/bus/pci/X/Y mmap()s,
  245. * modelled on the sparc64 implementation by Dave Miller.
  246. * -- paulus.
  247. */
  248. /*
  249. * Adjust vm_pgoff of VMA such that it is the physical page offset
  250. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  251. *
  252. * Basically, the user finds the base address for his device which he wishes
  253. * to mmap. They read the 32-bit value from the config space base register,
  254. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  255. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  256. *
  257. * Returns negative error code on failure, zero on success.
  258. */
  259. static __inline__ int
  260. __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
  261. enum pci_mmap_state mmap_state)
  262. {
  263. struct pci_controller *pci_ctrl = (struct pci_controller*) dev->sysdata;
  264. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  265. unsigned long io_offset = 0;
  266. int i, res_bit;
  267. if (pci_ctrl == 0)
  268. return -EINVAL; /* should never happen */
  269. /* If memory, add on the PCI bridge address offset */
  270. if (mmap_state == pci_mmap_mem) {
  271. res_bit = IORESOURCE_MEM;
  272. } else {
  273. io_offset = (unsigned long)pci_ctrl->io_space.base;
  274. offset += io_offset;
  275. res_bit = IORESOURCE_IO;
  276. }
  277. /*
  278. * Check that the offset requested corresponds to one of the
  279. * resources of the device.
  280. */
  281. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  282. struct resource *rp = &dev->resource[i];
  283. int flags = rp->flags;
  284. /* treat ROM as memory (should be already) */
  285. if (i == PCI_ROM_RESOURCE)
  286. flags |= IORESOURCE_MEM;
  287. /* Active and same type? */
  288. if ((flags & res_bit) == 0)
  289. continue;
  290. /* In the range of this resource? */
  291. if (offset < (rp->start & PAGE_MASK) || offset > rp->end)
  292. continue;
  293. /* found it! construct the final physical address */
  294. if (mmap_state == pci_mmap_io)
  295. offset += pci_ctrl->io_space.start - io_offset;
  296. vma->vm_pgoff = offset >> PAGE_SHIFT;
  297. return 0;
  298. }
  299. return -EINVAL;
  300. }
  301. /*
  302. * Set vm_flags of VMA, as appropriate for this architecture, for a pci device
  303. * mapping.
  304. */
  305. static __inline__ void
  306. __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
  307. enum pci_mmap_state mmap_state)
  308. {
  309. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  310. }
  311. /*
  312. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  313. * device mapping.
  314. */
  315. static __inline__ void
  316. __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
  317. enum pci_mmap_state mmap_state, int write_combine)
  318. {
  319. int prot = pgprot_val(vma->vm_page_prot);
  320. /* Set to write-through */
  321. prot &= ~_PAGE_NO_CACHE;
  322. #if 0
  323. if (!write_combine)
  324. prot |= _PAGE_WRITETHRU;
  325. #endif
  326. vma->vm_page_prot = __pgprot(prot);
  327. }
  328. /*
  329. * Perform the actual remap of the pages for a PCI device mapping, as
  330. * appropriate for this architecture. The region in the process to map
  331. * is described by vm_start and vm_end members of VMA, the base physical
  332. * address is found in vm_pgoff.
  333. * The pci device structure is provided so that architectures may make mapping
  334. * decisions on a per-device or per-bus basis.
  335. *
  336. * Returns a negative error code on failure, zero on success.
  337. */
  338. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  339. enum pci_mmap_state mmap_state,
  340. int write_combine)
  341. {
  342. int ret;
  343. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  344. if (ret < 0)
  345. return ret;
  346. __pci_mmap_set_flags(dev, vma, mmap_state);
  347. __pci_mmap_set_pgprot(dev, vma, mmap_state, write_combine);
  348. ret = io_remap_page_range(vma, vma->vm_start, vma->vm_pgoff<<PAGE_SHIFT,
  349. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  350. return ret;
  351. }
  352. /*
  353. * This probably belongs here rather than ioport.c because
  354. * we do not want this crud linked into SBus kernels.
  355. * Also, think for a moment about likes of floppy.c that
  356. * include architecture specific parts. They may want to redefine ins/outs.
  357. *
  358. * We do not use horroble macroses here because we want to
  359. * advance pointer by sizeof(size).
  360. */
  361. void outsb(unsigned long addr, const void *src, unsigned long count) {
  362. while (count) {
  363. count -= 1;
  364. writeb(*(const char *)src, addr);
  365. src += 1;
  366. addr += 1;
  367. }
  368. }
  369. void outsw(unsigned long addr, const void *src, unsigned long count) {
  370. while (count) {
  371. count -= 2;
  372. writew(*(const short *)src, addr);
  373. src += 2;
  374. addr += 2;
  375. }
  376. }
  377. void outsl(unsigned long addr, const void *src, unsigned long count) {
  378. while (count) {
  379. count -= 4;
  380. writel(*(const long *)src, addr);
  381. src += 4;
  382. addr += 4;
  383. }
  384. }
  385. void insb(unsigned long addr, void *dst, unsigned long count) {
  386. while (count) {
  387. count -= 1;
  388. *(unsigned char *)dst = readb(addr);
  389. dst += 1;
  390. addr += 1;
  391. }
  392. }
  393. void insw(unsigned long addr, void *dst, unsigned long count) {
  394. while (count) {
  395. count -= 2;
  396. *(unsigned short *)dst = readw(addr);
  397. dst += 2;
  398. addr += 2;
  399. }
  400. }
  401. void insl(unsigned long addr, void *dst, unsigned long count) {
  402. while (count) {
  403. count -= 4;
  404. /*
  405. * XXX I am sure we are in for an unaligned trap here.
  406. */
  407. *(unsigned long *)dst = readl(addr);
  408. dst += 4;
  409. addr += 4;
  410. }
  411. }