pci-sh5.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /* Rounds a number UP to the nearest power of two. Used for
  28. * sizing the PCI window.
  29. */
  30. static u32 __init r2p2(u32 num)
  31. {
  32. int i = 31;
  33. u32 tmp = num;
  34. if (num == 0)
  35. return 0;
  36. do {
  37. if (tmp & (1 << 31))
  38. break;
  39. i--;
  40. tmp <<= 1;
  41. } while (i >= 0);
  42. tmp = 1 << i;
  43. /* If the original number isn't a power of 2, round it up */
  44. if (tmp != num)
  45. tmp <<= 1;
  46. return tmp;
  47. }
  48. static irqreturn_t pcish5_err_irq(int irq, void *dev_id)
  49. {
  50. struct pt_regs *regs = get_irq_regs();
  51. unsigned pci_int, pci_air, pci_cir, pci_aint;
  52. pci_int = SH5PCI_READ(INT);
  53. pci_cir = SH5PCI_READ(CIR);
  54. pci_air = SH5PCI_READ(AIR);
  55. if (pci_int) {
  56. printk("PCI INTERRUPT (at %08llx)!\n", regs->pc);
  57. printk("PCI INT -> 0x%x\n", pci_int & 0xffff);
  58. printk("PCI AIR -> 0x%x\n", pci_air);
  59. printk("PCI CIR -> 0x%x\n", pci_cir);
  60. SH5PCI_WRITE(INT, ~0);
  61. }
  62. pci_aint = SH5PCI_READ(AINT);
  63. if (pci_aint) {
  64. printk("PCI ARB INTERRUPT!\n");
  65. printk("PCI AINT -> 0x%x\n", pci_aint);
  66. printk("PCI AIR -> 0x%x\n", pci_air);
  67. printk("PCI CIR -> 0x%x\n", pci_cir);
  68. SH5PCI_WRITE(AINT, ~0);
  69. }
  70. return IRQ_HANDLED;
  71. }
  72. static irqreturn_t pcish5_serr_irq(int irq, void *dev_id)
  73. {
  74. printk("SERR IRQ\n");
  75. return IRQ_NONE;
  76. }
  77. static struct resource sh5_pci_resources[2];
  78. static struct pci_channel sh5pci_controller = {
  79. .pci_ops = &sh5_pci_ops,
  80. .resources = sh5_pci_resources,
  81. .nr_resources = ARRAY_SIZE(sh5_pci_resources),
  82. .mem_offset = 0x00000000,
  83. .io_offset = 0x00000000,
  84. };
  85. static int __init sh5pci_init(void)
  86. {
  87. unsigned long memStart = __pa(memory_start);
  88. unsigned long memSize = __pa(memory_end) - memStart;
  89. u32 lsr0;
  90. u32 uval;
  91. if (request_irq(IRQ_ERR, pcish5_err_irq,
  92. 0, "PCI Error",NULL) < 0) {
  93. printk(KERN_ERR "PCISH5: Cannot hook PCI_PERR interrupt\n");
  94. return -EINVAL;
  95. }
  96. if (request_irq(IRQ_SERR, pcish5_serr_irq,
  97. 0, "PCI SERR interrupt", NULL) < 0) {
  98. printk(KERN_ERR "PCISH5: Cannot hook PCI_SERR interrupt\n");
  99. return -EINVAL;
  100. }
  101. pcicr_virt = (unsigned long)ioremap_nocache(SH5PCI_ICR_BASE, 1024);
  102. if (!pcicr_virt) {
  103. panic("Unable to remap PCICR\n");
  104. }
  105. PCI_IO_AREA = (unsigned long)ioremap_nocache(SH5PCI_IO_BASE, 0x10000);
  106. if (!PCI_IO_AREA) {
  107. panic("Unable to remap PCIIO\n");
  108. }
  109. /* Clear snoop registers */
  110. SH5PCI_WRITE(CSCR0, 0);
  111. SH5PCI_WRITE(CSCR1, 0);
  112. /* Switch off interrupts */
  113. SH5PCI_WRITE(INTM, 0);
  114. SH5PCI_WRITE(AINTM, 0);
  115. SH5PCI_WRITE(PINTM, 0);
  116. /* Set bus active, take it out of reset */
  117. uval = SH5PCI_READ(CR);
  118. /* Set command Register */
  119. SH5PCI_WRITE(CR, uval | CR_LOCK_MASK | CR_CFINT| CR_FTO | CR_PFE |
  120. CR_PFCS | CR_BMAM);
  121. uval=SH5PCI_READ(CR);
  122. /* Allow it to be a master */
  123. /* NB - WE DISABLE I/O ACCESS to stop overlap */
  124. /* set WAIT bit to enable stepping, an attempt to improve stability */
  125. SH5PCI_WRITE_SHORT(CSR_CMD,
  126. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  127. PCI_COMMAND_WAIT);
  128. /*
  129. ** Set translation mapping memory in order to convert the address
  130. ** used for the main bus, to the PCI internal address.
  131. */
  132. SH5PCI_WRITE(MBR,0x40000000);
  133. /* Always set the max size 512M */
  134. SH5PCI_WRITE(MBMR, PCISH5_MEM_SIZCONV(512*1024*1024));
  135. /*
  136. ** I/O addresses are mapped at internal PCI specific address
  137. ** as is described into the configuration bridge table.
  138. ** These are changed to 0, to allow cards that have legacy
  139. ** io such as vga to function correctly. We set the SH5 IOBAR to
  140. ** 256K, which is a bit big as we can only have 64K of address space
  141. */
  142. SH5PCI_WRITE(IOBR,0x0);
  143. /* Set up a 256K window. Totally pointless waste of address space */
  144. SH5PCI_WRITE(IOBMR,0);
  145. /* The SH5 has a HUGE 256K I/O region, which breaks the PCI spec.
  146. * Ideally, we would want to map the I/O region somewhere, but it
  147. * is so big this is not that easy!
  148. */
  149. SH5PCI_WRITE(CSR_IBAR0,~0);
  150. /* Set memory size value */
  151. memSize = memory_end - memory_start;
  152. /* Now we set up the mbars so the PCI bus can see the memory of
  153. * the machine */
  154. if (memSize < (1024 * 1024)) {
  155. printk(KERN_ERR "PCISH5: Ridiculous memory size of 0x%lx?\n",
  156. memSize);
  157. return -EINVAL;
  158. }
  159. /* Set LSR 0 */
  160. lsr0 = (memSize > (512 * 1024 * 1024)) ? 0x1ff00001 :
  161. ((r2p2(memSize) - 0x100000) | 0x1);
  162. SH5PCI_WRITE(LSR0, lsr0);
  163. /* Set MBAR 0 */
  164. SH5PCI_WRITE(CSR_MBAR0, memory_start);
  165. SH5PCI_WRITE(LAR0, memory_start);
  166. SH5PCI_WRITE(CSR_MBAR1,0);
  167. SH5PCI_WRITE(LAR1,0);
  168. SH5PCI_WRITE(LSR1,0);
  169. /* Enable the PCI interrupts on the device */
  170. SH5PCI_WRITE(INTM, ~0);
  171. SH5PCI_WRITE(AINTM, ~0);
  172. SH5PCI_WRITE(PINTM, ~0);
  173. sh5_pci_resources[0].start = PCI_IO_AREA;
  174. sh5_pci_resources[0].end = PCI_IO_AREA + 0x10000;
  175. sh5_pci_resources[1].start = memStart;
  176. sh5_pci_resources[1].end = memStart + memSize;
  177. return register_pci_controller(&sh5pci_controller);
  178. }
  179. arch_initcall(sh5pci_init);