pci-sh5.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2001 David J. Mckay (david.mckay@st.com)
  3. * Copyright (C) 2003, 2004 Paul Mundt
  4. * Copyright (C) 2004 Richard Curnow
  5. *
  6. * May be copied or modified under the terms of the GNU General Public
  7. * License. See linux/COPYING for more information.
  8. *
  9. * Support functions for the SH5 PCI hardware.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/rwsem.h>
  13. #include <linux/smp.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/init.h>
  16. #include <linux/errno.h>
  17. #include <linux/pci.h>
  18. #include <linux/delay.h>
  19. #include <linux/types.h>
  20. #include <linux/irq.h>
  21. #include <cpu/irq.h>
  22. #include <asm/pci.h>
  23. #include <asm/io.h>
  24. #include "pci-sh5.h"
  25. unsigned long pcicr_virt;
  26. unsigned long PCI_IO_AREA;
  27. int __init sh5_pci_init(struct pci_channel *chan)
  28. {
  29. pr_debug("PCI: Starting intialization.\n");
  30. return pcibios_init_platform();
  31. }
  32. /* Rounds a number UP to the nearest power of two. Used for
  33. * sizing the PCI window.
  34. */
  35. static u32 __init r2p2(u32 num)
  36. {
  37. int i = 31;
  38. u32 tmp = num;
  39. if (num == 0)
  40. return 0;
  41. do {
  42. if (tmp & (1 << 31))
  43. break;
  44. i--;
  45. tmp <<= 1;
  46. } while (i >= 0);
  47. tmp = 1 << i;
  48. /* If the original number isn't a power of 2, round it up */
  49. if (tmp != num)
  50. tmp <<= 1;
  51. return tmp;
  52. }
  53. static irqreturn_t pcish5_err_irq(int irq, void *dev_id)
  54. {
  55. struct pt_regs *regs = get_irq_regs();
  56. unsigned pci_int, pci_air, pci_cir, pci_aint;
  57. pci_int = SH5PCI_READ(INT);
  58. pci_cir = SH5PCI_READ(CIR);
  59. pci_air = SH5PCI_READ(AIR);
  60. if (pci_int) {
  61. printk("PCI INTERRUPT (at %08llx)!\n", regs->pc);
  62. printk("PCI INT -> 0x%x\n", pci_int & 0xffff);
  63. printk("PCI AIR -> 0x%x\n", pci_air);
  64. printk("PCI CIR -> 0x%x\n", pci_cir);
  65. SH5PCI_WRITE(INT, ~0);
  66. }
  67. pci_aint = SH5PCI_READ(AINT);
  68. if (pci_aint) {
  69. printk("PCI ARB INTERRUPT!\n");
  70. printk("PCI AINT -> 0x%x\n", pci_aint);
  71. printk("PCI AIR -> 0x%x\n", pci_air);
  72. printk("PCI CIR -> 0x%x\n", pci_cir);
  73. SH5PCI_WRITE(AINT, ~0);
  74. }
  75. return IRQ_HANDLED;
  76. }
  77. static irqreturn_t pcish5_serr_irq(int irq, void *dev_id)
  78. {
  79. printk("SERR IRQ\n");
  80. return IRQ_NONE;
  81. }
  82. int __init sh5pci_init(unsigned long memStart, unsigned long memSize)
  83. {
  84. u32 lsr0;
  85. u32 uval;
  86. if (request_irq(IRQ_ERR, pcish5_err_irq,
  87. IRQF_DISABLED, "PCI Error",NULL) < 0) {
  88. printk(KERN_ERR "PCISH5: Cannot hook PCI_PERR interrupt\n");
  89. return -EINVAL;
  90. }
  91. if (request_irq(IRQ_SERR, pcish5_serr_irq,
  92. IRQF_DISABLED, "PCI SERR interrupt", NULL) < 0) {
  93. printk(KERN_ERR "PCISH5: Cannot hook PCI_SERR interrupt\n");
  94. return -EINVAL;
  95. }
  96. pcicr_virt = onchip_remap(SH5PCI_ICR_BASE, 1024, "PCICR");
  97. if (!pcicr_virt) {
  98. panic("Unable to remap PCICR\n");
  99. }
  100. PCI_IO_AREA = onchip_remap(SH5PCI_IO_BASE, 0x10000, "PCIIO");
  101. if (!PCI_IO_AREA) {
  102. panic("Unable to remap PCIIO\n");
  103. }
  104. /* Clear snoop registers */
  105. SH5PCI_WRITE(CSCR0, 0);
  106. SH5PCI_WRITE(CSCR1, 0);
  107. /* Switch off interrupts */
  108. SH5PCI_WRITE(INTM, 0);
  109. SH5PCI_WRITE(AINTM, 0);
  110. SH5PCI_WRITE(PINTM, 0);
  111. /* Set bus active, take it out of reset */
  112. uval = SH5PCI_READ(CR);
  113. /* Set command Register */
  114. SH5PCI_WRITE(CR, uval | CR_LOCK_MASK | CR_CFINT| CR_FTO | CR_PFE |
  115. CR_PFCS | CR_BMAM);
  116. uval=SH5PCI_READ(CR);
  117. /* Allow it to be a master */
  118. /* NB - WE DISABLE I/O ACCESS to stop overlap */
  119. /* set WAIT bit to enable stepping, an attempt to improve stability */
  120. SH5PCI_WRITE_SHORT(CSR_CMD,
  121. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  122. PCI_COMMAND_WAIT);
  123. /*
  124. ** Set translation mapping memory in order to convert the address
  125. ** used for the main bus, to the PCI internal address.
  126. */
  127. SH5PCI_WRITE(MBR,0x40000000);
  128. /* Always set the max size 512M */
  129. SH5PCI_WRITE(MBMR, PCISH5_MEM_SIZCONV(512*1024*1024));
  130. /*
  131. ** I/O addresses are mapped at internal PCI specific address
  132. ** as is described into the configuration bridge table.
  133. ** These are changed to 0, to allow cards that have legacy
  134. ** io such as vga to function correctly. We set the SH5 IOBAR to
  135. ** 256K, which is a bit big as we can only have 64K of address space
  136. */
  137. SH5PCI_WRITE(IOBR,0x0);
  138. /* Set up a 256K window. Totally pointless waste of address space */
  139. SH5PCI_WRITE(IOBMR,0);
  140. /* The SH5 has a HUGE 256K I/O region, which breaks the PCI spec.
  141. * Ideally, we would want to map the I/O region somewhere, but it
  142. * is so big this is not that easy!
  143. */
  144. SH5PCI_WRITE(CSR_IBAR0,~0);
  145. /* Set memory size value */
  146. memSize = memory_end - memory_start;
  147. /* Now we set up the mbars so the PCI bus can see the memory of
  148. * the machine */
  149. if (memSize < (1024 * 1024)) {
  150. printk(KERN_ERR "PCISH5: Ridiculous memory size of 0x%lx?\n",
  151. memSize);
  152. return -EINVAL;
  153. }
  154. /* Set LSR 0 */
  155. lsr0 = (memSize > (512 * 1024 * 1024)) ? 0x1ff00001 :
  156. ((r2p2(memSize) - 0x100000) | 0x1);
  157. SH5PCI_WRITE(LSR0, lsr0);
  158. /* Set MBAR 0 */
  159. SH5PCI_WRITE(CSR_MBAR0, memory_start);
  160. SH5PCI_WRITE(LAR0, memory_start);
  161. SH5PCI_WRITE(CSR_MBAR1,0);
  162. SH5PCI_WRITE(LAR1,0);
  163. SH5PCI_WRITE(LSR1,0);
  164. /* Enable the PCI interrupts on the device */
  165. SH5PCI_WRITE(INTM, ~0);
  166. SH5PCI_WRITE(AINTM, ~0);
  167. SH5PCI_WRITE(PINTM, ~0);
  168. return 0;
  169. }
  170. #define xPCIBIOS_MIN_IO board_pci_channels->io_resource->start
  171. #define xPCIBIOS_MIN_MEM board_pci_channels->mem_resource->start
  172. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  173. {
  174. struct pci_dev *dev = bus->self;
  175. int i;
  176. if (dev) {
  177. for (i= 0; i < 3; i++) {
  178. bus->resource[i] =
  179. &dev->resource[PCI_BRIDGE_RESOURCES+i];
  180. bus->resource[i]->name = bus->name;
  181. }
  182. bus->resource[0]->flags |= IORESOURCE_IO;
  183. bus->resource[1]->flags |= IORESOURCE_MEM;
  184. /* For now, propagate host limits to the bus;
  185. * we'll adjust them later. */
  186. bus->resource[0]->end = 64*1024 - 1 ;
  187. bus->resource[1]->end = xPCIBIOS_MIN_MEM+(256*1024*1024)-1;
  188. bus->resource[0]->start = xPCIBIOS_MIN_IO;
  189. bus->resource[1]->start = xPCIBIOS_MIN_MEM;
  190. /* Turn off downstream PF memory address range by default */
  191. bus->resource[2]->start = 1024*1024;
  192. bus->resource[2]->end = bus->resource[2]->start - 1;
  193. }
  194. }