ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / pci / pci-vr41xx.c
1 /*
2  * FILE NAME
3  *      arch/mips/vr41xx/common/pciu.c
4  *
5  * BRIEF MODULE DESCRIPTION
6  *      PCI Control Unit routines for the NEC VR4100 series.
7  *
8  * Author: Yoichi Yuasa
9  *         yyuasa@mvista.com or source@mvista.com
10  *
11  * Copyright 2001-2003 MontaVista Software Inc.
12  *
13  *  This program is free software; you can redistribute it and/or modify it
14  *  under the terms of the GNU General Public License as published by the
15  *  Free Software Foundation; either version 2 of the License, or (at your
16  *  option) any later version.
17  *
18  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *  You should have received a copy of the GNU General Public License along
30  *  with this program; if not, write to the Free Software Foundation, Inc.,
31  *  675 Mass Ave, Cambridge, MA 02139, USA.
32  */
33 /*
34  * Changes:
35  *  Paul Mundt <lethal@chaoticdreams.org>
36  *  - Fix deadlock-causing PCIU access race for VR4131.
37  *
38  *  MontaVista Software Inc. <yyuasa@mvista.com> or <source@mvista.com>
39  *  - New creation, NEC VR4122 and VR4131 are supported.
40  */
41 #include <linux/config.h>
42 #include <linux/init.h>
43 #include <linux/pci.h>
44 #include <linux/types.h>
45 #include <linux/delay.h>
46
47 #include <asm/cpu.h>
48 #include <asm/io.h>
49 #include <asm/pci_channel.h>
50 #include <asm/vr41xx/vr41xx.h>
51
52 #include "pci-vr41xx.h"
53
54 static inline int vr41xx_pci_config_access(unsigned char bus,
55                                            unsigned int devfn, int where)
56 {
57         if (bus == 0) {
58                 /*
59                  * Type 0 configuration
60                  */
61                 if (PCI_SLOT(devfn) < 11 || where > 255)
62                         return -1;
63
64                 writel((1UL << PCI_SLOT(devfn)) |
65                        (PCI_FUNC(devfn) << 8) |
66                        (where & 0xfc), PCICONFAREG);
67         } else {
68                 /*
69                  * Type 1 configuration
70                  */
71                 if (where > 255)
72                         return -1;
73
74                 writel((bus << 16) |
75                        (devfn << 8) | (where & 0xfc) | 1UL, PCICONFAREG);
76         }
77
78         return 0;
79 }
80
81 static int vr41xx_pci_config_read(struct pci_bus *bus, unsigned int devfn,
82                                   int where, int size, u32 * val)
83 {
84         u32 data;
85
86         *val = 0xffffffffUL;
87         if (vr41xx_pci_config_access(bus->number, devfn, where) < 0)
88                 return PCIBIOS_DEVICE_NOT_FOUND;
89
90         data = readl(PCICONFDREG);
91
92         switch (size) {
93         case 1:
94                 *val = (data >> ((where & 3) << 3)) & 0xffUL;
95                 break;
96         case 2:
97                 *val = (data >> ((where & 2) << 3)) & 0xffffUL;
98                 break;
99         case 4:
100                 *val = data;
101                 break;
102         default:
103                 return PCIBIOS_FUNC_NOT_SUPPORTED;
104         }
105
106         return PCIBIOS_SUCCESSFUL;
107 }
108
109 static int vr41xx_pci_config_write(struct pci_bus *bus, unsigned int devfn,
110                                    int where, int size, u32 val)
111 {
112         u32 data;
113         int shift;
114
115         if (vr41xx_pci_config_access(bus->number, devfn, where) < 0)
116                 return PCIBIOS_DEVICE_NOT_FOUND;
117
118         data = readl(PCICONFDREG);
119
120         switch (size) {
121         case 1:
122                 shift = (where & 3) << 3;
123                 data &= ~(0xff << shift);
124                 data |= ((val & 0xff) << shift);
125                 break;
126         case 2:
127                 shift = (where & 2) << 3;
128                 data &= ~(0xffff << shift);
129                 data |= ((val & 0xffff) << shift);
130                 break;
131         case 4:
132                 data = val;
133                 break;
134         default:
135                 return PCIBIOS_FUNC_NOT_SUPPORTED;
136         }
137
138         writel(data, PCICONFDREG);
139
140         return PCIBIOS_SUCCESSFUL;
141 }
142
143 struct pci_ops vr41xx_pci_ops = {
144         .read = vr41xx_pci_config_read,
145         .write = vr41xx_pci_config_write,
146 };
147
148 void __init vr41xx_pciu_init(struct vr41xx_pci_address_map *map)
149 {
150         struct vr41xx_pci_address_space *s;
151         unsigned long vtclock;
152         u32 config;
153         int n;
154
155         if (!map)
156                 return;
157
158         /* Disable PCI interrupt */
159         writew(0, MPCIINTREG);
160
161         /* Supply VTClock to PCIU */
162         vr41xx_supply_clock(PCIU_CLOCK);
163
164         /*
165          * Sleep for 1us after setting MSKPPCIU bit in CMUCLKMSK
166          * before doing any PCIU access to avoid deadlock on VR4131.
167          */
168         udelay(1);
169
170         /* Select PCI clock */
171         vtclock = vr41xx_get_vtclock_frequency();
172         if (vtclock < MAX_PCI_CLOCK)
173                 writel(EQUAL_VTCLOCK, PCICLKSELREG);
174         else if ((vtclock / 2) < MAX_PCI_CLOCK)
175                 writel(HALF_VTCLOCK, PCICLKSELREG);
176         else if ((vtclock / 4) < MAX_PCI_CLOCK)
177                 writel(QUARTER_VTCLOCK, PCICLKSELREG);
178         else
179                 printk(KERN_INFO "Warning: PCI Clock is over 33MHz.\n");
180
181         /* Supply PCI clock by PCI bus */
182         vr41xx_supply_clock(PCI_CLOCK);
183
184         /*
185          * Set PCI memory & I/O space address conversion registers
186          * for master transaction.
187          */
188         if (map->mem1 != NULL) {
189                 s = map->mem1;
190                 config = (s->internal_base & 0xff000000) |
191                     ((s->address_mask & 0x7f000000) >> 11) | (1UL << 12) |
192                     ((s->pci_base & 0xff000000) >> 24);
193                 writel(config, PCIMMAW1REG);
194         }
195         if (map->mem2 != NULL) {
196                 s = map->mem2;
197                 config = (s->internal_base & 0xff000000) |
198                     ((s->address_mask & 0x7f000000) >> 11) | (1UL << 12) |
199                     ((s->pci_base & 0xff000000) >> 24);
200                 writel(config, PCIMMAW2REG);
201         }
202         if (map->io != NULL) {
203                 s = map->io;
204                 config = (s->internal_base & 0xff000000) |
205                     ((s->address_mask & 0x7f000000) >> 11) | (1UL << 12) |
206                     ((s->pci_base & 0xff000000) >> 24);
207                 writel(config, PCIMIOAWREG);
208         }
209
210         /* Set target memory windows */
211         writel(0x00081000, PCITAW1REG);
212         writel(0UL, PCITAW2REG);
213         pciu_write_config_dword(PCI_BASE_ADDRESS_0, 0UL);
214         pciu_write_config_dword(PCI_BASE_ADDRESS_1, 0UL);
215
216         /* Clear bus error */
217         n = readl(BUSERRADREG);
218
219         if (current_cpu_data.cputype == CPU_VR4122) {
220                 writel(0UL, PCITRDYVREG);
221                 pciu_write_config_dword(PCI_CACHE_LINE_SIZE, 0x0000f804);
222         } else {
223                 writel(100UL, PCITRDYVREG);
224                 pciu_write_config_dword(PCI_CACHE_LINE_SIZE, 0x00008004);
225         }
226
227         writel(CONFIG_DONE, PCIENREG);
228         pciu_write_config_dword(PCI_COMMAND,
229                                 PCI_COMMAND_IO |
230                                 PCI_COMMAND_MEMORY |
231                                 PCI_COMMAND_MASTER |
232                                 PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
233 }