ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / pci / ops-au1000.c
1 /*
2  * BRIEF MODULE DESCRIPTION
3  *      Alchemy/AMD Au1x00 pci support.
4  *
5  * Copyright 2001,2002,2003 MontaVista Software Inc.
6  * Author: MontaVista Software, Inc.
7  *              ppopov@mvista.com or source@mvista.com
8  *
9  *  Support for all devices (greater than 16) added by David Gathright.
10  *
11  *  This program is free software; you can redistribute  it and/or modify it
12  *  under  the terms of  the GNU General  Public License as published by the
13  *  Free Software Foundation;  either version 2 of the  License, or (at your
14  *  option) any later version.
15  *
16  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
17  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
18  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
19  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
20  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
22  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
24  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  *  You should have received a copy of the  GNU General Public License along
28  *  with this program; if not, write  to the Free Software Foundation, Inc.,
29  *  675 Mass Ave, Cambridge, MA 02139, USA.
30  */
31 #include <linux/config.h>
32 #include <linux/types.h>
33 #include <linux/pci.h>
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36
37 #include <asm/mach-au1x00/au1000.h>
38
39 #define PCI_ACCESS_READ  0
40 #define PCI_ACCESS_WRITE 1
41
42
43 static int config_access(unsigned char access_type, struct pci_bus *bus,
44                          unsigned int devfn, unsigned char where,
45                          u32 * data)
46 {
47         unsigned int device = PCI_SLOT(devfn);
48         unsigned int function = PCI_FUNC(devfn);
49         unsigned long config, status;
50         unsigned long cfg_addr;
51
52         if (device > 19) {
53                 *data = 0xffffffff;
54                 return -1;
55         }
56
57         au_writel(((0x2000 << 16) |
58                    (au_readl(Au1500_PCI_STATCMD) & 0xffff)),
59                   Au1500_PCI_STATCMD);
60         //au_writel(au_readl(Au1500_PCI_CFG) & ~PCI_ERROR, Au1500_PCI_CFG);
61         au_sync_udelay(1);
62
63         /* setup the config window */
64         if (bus->number == 0) {
65                 cfg_addr = (unsigned long) ioremap(Au1500_EXT_CFG |
66                                                    ((1 << device) << 11),
67                                                    0x00100000);
68         } else {
69                 cfg_addr = (unsigned long) ioremap(Au1500_EXT_CFG_TYPE1 |
70                                                    (bus->
71                                                     number << 16) | (device
72                                                                      <<
73                                                                      11),
74                                                    0x00100000);
75         }
76
77         if (!cfg_addr)
78                 panic(KERN_ERR "PCI unable to ioremap cfg space\n");
79
80         /* setup the lower bits of the 36 bit address */
81         config = cfg_addr | (function << 8) | (where & ~0x3);
82
83 #if 1
84         if (access_type == PCI_ACCESS_WRITE) {
85                 printk("cfg write:  ");
86         } else {
87                 printk("cfg read:  ");
88         }
89         printk("devfn %x, device %x func %x  \n", devfn, device, function);
90         if (access_type == PCI_ACCESS_WRITE) {
91                 printk("data %x\n", *data);
92         }
93 #endif
94
95         if (access_type == PCI_ACCESS_WRITE) {
96                 au_writel(*data, config);
97         } else {
98                 *data = au_readl(config);
99         }
100         au_sync_udelay(2);
101
102         /* unmap io space */
103         iounmap((void *) cfg_addr);
104
105         /* check master abort */
106         status = au_readl(Au1500_PCI_STATCMD);
107         if (status & (1 << 29)) {
108                 printk("master abort\n");
109                 *data = 0xffffffff;
110                 return -1;
111         } else if ((status >> 28) & 0xf) {
112                 printk("PCI ERR detected: status %x\n", status);
113                 *data = 0xffffffff;
114                 return -1;
115         } else {
116                 printk("bios_successful: %x\n", *data);
117                 return PCIBIOS_SUCCESSFUL;
118         }
119 }
120
121 static int read_config_byte(struct pci_bus *bus, unsigned int devfn,
122                             int where, u8 * val)
123 {
124         u32 data;
125         int ret;
126
127         ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
128         if (where & 1)
129                 data >>= 8;
130         if (where & 2)
131                 data >>= 16;
132         *val = data & 0xff;
133         return ret;
134 }
135
136
137 static int read_config_word(struct pci_bus *bus, unsigned int devfn,
138                             int where, u16 * val)
139 {
140         u32 data;
141         int ret;
142
143         ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
144         if (where & 2)
145                 data >>= 16;
146         *val = data & 0xffff;
147         return ret;
148 }
149
150 static int read_config_dword(struct pci_bus *bus, unsigned int devfn,
151                              int where, u32 * val)
152 {
153         int ret;
154
155         ret = config_access(PCI_ACCESS_READ, bus, devfn, where, val);
156         return ret;
157 }
158
159 static int
160 write_config_byte(struct pci_bus *bus, unsigned int devfn, int where,
161                   u8 val)
162 {
163         u32 data = 0;
164
165         if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
166                 return -1;
167
168         data = (data & ~(0xff << ((where & 3) << 3))) |
169             (val << ((where & 3) << 3));
170
171         if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
172                 return -1;
173
174         return PCIBIOS_SUCCESSFUL;
175 }
176
177 static int
178 write_config_word(struct pci_bus *bus, unsigned int devfn, int where,
179                   u16 val)
180 {
181         u32 data = 0;
182
183         if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
184                 return -1;
185
186         data = (data & ~(0xffff << ((where & 3) << 3))) |
187             (val << ((where & 3) << 3));
188
189         if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
190                 return -1;
191
192
193         return PCIBIOS_SUCCESSFUL;
194 }
195
196 static int
197 write_config_dword(struct pci_bus *bus, unsigned int devfn, int where,
198                    u32 val)
199 {
200         if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &val))
201                 return -1;
202
203         return PCIBIOS_SUCCESSFUL;
204 }
205
206 static int config_read(struct pci_bus *bus, unsigned int devfn,
207                        int where, int size, u32 * val)
208 {
209         switch (size) {
210         case 1:
211                 return read_config_byte(bus, devfn, where, (u8 *) val);
212         case 2:
213                 return read_config_word(bus, devfn, where, (u16 *) val);
214         default:
215                 return read_config_dword(bus, devfn, where, val);
216         }
217 }
218
219 static int config_write(struct pci_bus *bus, unsigned int devfn,
220                         int where, int size, u32 val)
221 {
222         switch (size) {
223         case 1:
224                 return write_config_byte(bus, devfn, where, (u8) val);
225         case 2:
226                 return write_config_word(bus, devfn, where, (u16) val);
227         default:
228                 return write_config_dword(bus, devfn, where, val);
229         }
230 }
231
232
233 struct pci_ops au1x_pci_ops = {
234         config_read,
235         config_write
236 };