ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / syslib / ppc405_pci.c
1 /*
2  * Authors: Frank Rowand <frank_rowand@mvista.com>,
3  * Debbie Chu <debbie_chu@mvista.com>, or source@mvista.com
4  * Further modifications by Armin Kuster <akuster@mvista.com>
5  *
6  * 2000 (c) MontaVista, Software, Inc.  This file is licensed under
7  * the terms of the GNU General Public License version 2.  This program
8  * is licensed "as is" without any warranty of any kind, whether express
9  * or implied.
10  *
11  * Based on arch/ppc/kernel/indirect.c, Copyright (C) 1998 Gabriel Paubert.
12  */
13
14 #include <linux/pci.h>
15 #include <asm/io.h>
16 #include <asm/system.h>
17 #include <asm/machdep.h>
18 #include <linux/init.h>
19 #include <linux/errno.h>
20 #include <asm/ocp.h>
21 #include <asm/ibm4xx.h>
22 #include <asm/pci-bridge.h>
23 #include <asm/ibm_ocp_pci.h>
24
25
26 extern void bios_fixup(struct pci_controller *, struct pcil0_regs *);
27 extern int ppc405_map_irq(struct pci_dev *dev, unsigned char idsel,
28                           unsigned char pin);
29
30 void
31 ppc405_pcibios_fixup_resources(struct pci_dev *dev)
32 {
33         int i;
34         unsigned long max_host_addr;
35         unsigned long min_host_addr;
36         struct resource *res;
37
38         /*
39          * openbios puts some graphics cards in the same range as the host
40          * controller uses to map to SDRAM.  Fix it.
41          */
42
43         min_host_addr = 0;
44         max_host_addr = PPC405_PCI_MEM_BASE - 1;
45
46         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
47                 res = dev->resource + i;
48                 if (!res->start)
49                         continue;
50                 if ((res->flags & IORESOURCE_MEM) &&
51                     (((res->start >= min_host_addr)
52                       && (res->start <= max_host_addr))
53                      || ((res->end >= min_host_addr)
54                          && (res->end <= max_host_addr))
55                      || ((res->start < min_host_addr)
56                          && (res->end > max_host_addr))
57                     )
58                     ) {
59
60                         DBG(KERN_ERR "PCI: 0x%lx <= resource[%d] <= 0x%lx"
61                             ", bus 0x%x dev 0x%2.2x.%1.1x,\n"
62                             KERN_ERR "  %s\n"
63                             KERN_ERR "  fixup will be attempted later\n",
64                             min_host_addr, i, max_host_addr,
65                             dev->bus->number, PCI_SLOT(dev->devfn),
66                             PCI_FUNC(dev->devfn), dev->slot.name);
67
68                         /* force pcibios_assign_resources() to assign a new address */
69                         res->end -= res->start;
70                         res->start = 0;
71                 }
72         }
73 }
74
75 static int
76 ppc4xx_exclude_device(unsigned char bus, unsigned char devfn)
77 {
78         /* We prevent us from seeing ourselves to avoid having
79          * the kernel try to remap our BAR #1 and fuck up bus
80          * master from external PCI devices
81          */
82         return (bus == 0 && devfn == 0);
83 }
84
85 void
86 ppc4xx_find_bridges(void)
87 {
88         struct pci_controller *hose_a;
89         struct pcil0_regs *pcip;
90         unsigned int tmp_addr;
91         unsigned int tmp_size;
92         unsigned int reg_index;
93         unsigned int new_pmm_max;
94         unsigned int new_pmm_min;
95
96         isa_io_base = 0;
97         isa_mem_base = 0;
98         pci_dram_offset = 0;
99
100 #if  (PSR_PCI_ARBIT_EN > 1)
101         /* Check if running in slave mode */
102         if ((mfdcr(DCRN_CHPSR) & PSR_PCI_ARBIT_EN) == 0) {
103                 printk("Running as PCI slave, kernel PCI disabled !\n");
104                 return;
105         }
106 #endif
107         /* Setup PCI32 hose */
108         hose_a = pcibios_alloc_controller();
109         if (!hose_a)
110                 return;
111         setup_indirect_pci(hose_a, PPC405_PCI_CONFIG_ADDR,
112                            PPC405_PCI_CONFIG_DATA);
113
114         pcip = ioremap(PPC4xx_PCI_LCFG_PADDR, PAGE_SIZE);
115         if (pcip != NULL) {
116
117 #if defined(CONFIG_BIOS_FIXUP)
118                 bios_fixup(hose_a, pcip);
119 #endif
120                 new_pmm_min = 0xffffffff;
121                 for (reg_index = 0; reg_index < 3; reg_index++) {
122                         tmp_size = in_le32(&pcip->pmm[reg_index].ma);   // mask & attrs
123                         /* test the enable bit */
124                         if ((tmp_size & 0x1) == 0)
125                                 continue;
126                         tmp_addr = in_le32(&pcip->pmm[reg_index].pcila);        // PCI addr
127                         if (tmp_addr < PPC405_PCI_PHY_MEM_BASE) {
128                                 printk(KERN_DEBUG
129                                        "Disabling mapping to PCI mem addr 0x%8.8x\n",
130                                        tmp_addr);
131                                 out_le32(&pcip->pmm[reg_index].ma, tmp_size & ~1);      // *_PMMOMA
132                                 continue;
133                         }
134                         tmp_addr = in_le32(&pcip->pmm[reg_index].la);   // *_PMMOLA
135                         if (tmp_addr < new_pmm_min)
136                                 new_pmm_min = tmp_addr;
137                         tmp_addr = tmp_addr +
138                                 (0xffffffff - (tmp_size & 0xffffc000));
139                         if (tmp_addr > PPC405_PCI_UPPER_MEM) {
140                                 new_pmm_max = tmp_addr; // PPC405_PCI_UPPER_MEM
141                         } else {
142                                 new_pmm_max = PPC405_PCI_UPPER_MEM;
143                         }
144
145                 }               // for
146
147                 iounmap(pcip);
148         }
149
150         hose_a->first_busno = 0;
151         hose_a->last_busno = 0xff;
152         hose_a->pci_mem_offset = 0;
153
154         /* Setup bridge memory/IO ranges & resources
155          * TODO: Handle firmwares setting up a legacy ISA mem base
156          */
157         hose_a->io_space.start = PPC405_PCI_LOWER_IO;
158         hose_a->io_space.end = PPC405_PCI_UPPER_IO;
159         hose_a->mem_space.start = new_pmm_min;
160         hose_a->mem_space.end = new_pmm_max;
161         hose_a->io_base_phys = PPC405_PCI_PHY_IO_BASE;
162         hose_a->io_base_virt = ioremap(hose_a->io_base_phys, 0x10000);
163         hose_a->io_resource.start = 0;
164         hose_a->io_resource.end = PPC405_PCI_UPPER_IO - PPC405_PCI_LOWER_IO;
165         hose_a->io_resource.flags = IORESOURCE_IO;
166         hose_a->io_resource.name = "PCI I/O";
167         hose_a->mem_resources[0].start = new_pmm_min;
168         hose_a->mem_resources[0].end = new_pmm_max;
169         hose_a->mem_resources[0].flags = IORESOURCE_MEM;
170         hose_a->mem_resources[0].name = "PCI Memory";
171         isa_io_base = (int) hose_a->io_base_virt;
172         isa_mem_base = 0;       /*     ISA not implemented */
173         ISA_DMA_THRESHOLD = 0x00ffffff; /* ??? ISA not implemented */
174
175         /* Scan busses & initial setup by pci_auto */
176         hose_a->last_busno = pciauto_bus_scan(hose_a, hose_a->first_busno);
177         hose_a->last_busno = 0;
178
179         /* Setup ppc_md */
180         ppc_md.pcibios_fixup = NULL;
181         ppc_md.pci_exclude_device = ppc4xx_exclude_device;
182         ppc_md.pcibios_fixup_resources = ppc405_pcibios_fixup_resources;
183         ppc_md.pci_swizzle = common_swizzle;
184         ppc_md.pci_map_irq = ppc405_map_irq;
185 }