VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / arch / mips / pci / ops-msc.c
1 /*
2  * Carsten Langgaard, carstenl@mips.com
3  * Copyright (C) 1999, 2000 MIPS Technologies, Inc.  All rights reserved.
4  *
5  *  This program is free software; you can distribute it and/or modify it
6  *  under the terms of the GNU General Public License (Version 2) as
7  *  published by the Free Software Foundation.
8  *
9  *  This program is distributed in the hope it will be useful, but WITHOUT
10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  *  for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
17  *
18  * MIPS boards specific PCI support.
19  *
20  */
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25
26 #include <asm/mips-boards/msc01_pci.h>
27
28 #define PCI_ACCESS_READ  0
29 #define PCI_ACCESS_WRITE 1
30
31 /*
32  *  PCI configuration cycle AD bus definition
33  */
34 /* Type 0 */
35 #define PCI_CFG_TYPE0_REG_SHF           0
36 #define PCI_CFG_TYPE0_FUNC_SHF          8
37
38 /* Type 1 */
39 #define PCI_CFG_TYPE1_REG_SHF           0
40 #define PCI_CFG_TYPE1_FUNC_SHF          8
41 #define PCI_CFG_TYPE1_DEV_SHF           11
42 #define PCI_CFG_TYPE1_BUS_SHF           16
43
44 static int msc_pcibios_config_access(unsigned char access_type,
45         struct pci_bus *bus, unsigned int devfn, int where, u32 * data)
46 {
47         unsigned char busnum = bus->number;
48         unsigned char type;
49         u32 intr;
50
51 #ifdef CONFIG_MIPS_BOARDS_GEN
52         if ((busnum == 0) && (PCI_SLOT(devfn) == 17)) {
53                 /* MIPS Core boards have SOCit connected as device 17 */
54                 return -1;
55         }
56 #endif
57
58         /* Clear status register bits. */
59         MSC_WRITE(MSC01_PCI_INTSTAT,
60                   (MSC01_PCI_INTCFG_MA_BIT | MSC01_PCI_INTCFG_TA_BIT));
61
62         /* Setup address */
63         if (busnum == 0)
64                 type = 0;       /* Type 0 */
65         else
66                 type = 1;       /* Type 1 */
67
68         MSC_WRITE(MSC01_PCI_CFGADDR,
69                   ((busnum << MSC01_PCI_CFGADDR_BNUM_SHF) |
70                    (PCI_SLOT(devfn) << MSC01_PCI_CFGADDR_DNUM_SHF)
71                    | (PCI_FUNC(devfn) <<
72                       MSC01_PCI_CFGADDR_FNUM_SHF) | ((where /
73                                                       4) <<
74                                                      MSC01_PCI_CFGADDR_RNUM_SHF)
75                    | (type)));
76
77         /* Perform access */
78         if (access_type == PCI_ACCESS_WRITE)
79                 MSC_WRITE(MSC01_PCI_CFGDATA, *data);
80         else
81                 MSC_READ(MSC01_PCI_CFGDATA, *data);
82
83         /* Detect Master/Target abort */
84         MSC_READ(MSC01_PCI_INTSTAT, intr);
85         if (intr & (MSC01_PCI_INTCFG_MA_BIT |
86                     MSC01_PCI_INTCFG_TA_BIT)) {
87                 /* Error occurred */
88
89                 /* Clear bits */
90                 MSC_READ(MSC01_PCI_INTSTAT, intr);
91                 MSC_WRITE(MSC01_PCI_INTSTAT,
92                           (MSC01_PCI_INTCFG_MA_BIT |
93                            MSC01_PCI_INTCFG_TA_BIT));
94
95                 return -1;
96         }
97
98         return 0;
99 }
100
101
102 /*
103  * We can't address 8 and 16 bit words directly.  Instead we have to
104  * read/write a 32bit word and mask/modify the data we actually want.
105  */
106 static int msc_pcibios_read(struct pci_bus *bus, unsigned int devfn,
107                              int where, int size, u32 * val)
108 {
109         u32 data = 0;
110
111         if ((size == 2) && (where & 1))
112                 return PCIBIOS_BAD_REGISTER_NUMBER;
113         else if ((size == 4) && (where & 3))
114                 return PCIBIOS_BAD_REGISTER_NUMBER;
115
116         if (msc_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
117                                       &data))
118                 return -1;
119
120         if (size == 1)
121                 *val = (data >> ((where & 3) << 3)) & 0xff;
122         else if (size == 2)
123                 *val = (data >> ((where & 3) << 3)) & 0xffff;
124         else
125                 *val = data;
126
127         return PCIBIOS_SUCCESSFUL;
128 }
129
130 static int msc_pcibios_write(struct pci_bus *bus, unsigned int devfn,
131                               int where, int size, u32 val)
132 {
133         u32 data = 0;
134
135         if ((size == 2) && (where & 1))
136                 return PCIBIOS_BAD_REGISTER_NUMBER;
137         else if ((size == 4) && (where & 3))
138                 return PCIBIOS_BAD_REGISTER_NUMBER;
139
140         if (size == 4)
141                 data = val;
142         else {
143                 if (msc_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
144                                               where, &data))
145                         return -1;
146
147                 if (size == 1)
148                         data = (data & ~(0xff << ((where & 3) << 3))) |
149                                 (val << ((where & 3) << 3));
150                 else if (size == 2)
151                         data = (data & ~(0xffff << ((where & 3) << 3))) |
152                                 (val << ((where & 3) << 3));
153         }
154
155         if (msc_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
156                                        &data))
157                 return -1;
158
159         return PCIBIOS_SUCCESSFUL;
160 }
161
162 struct pci_ops msc_pci_ops = {
163         .read = msc_pcibios_read,
164         .write = msc_pcibios_write
165 };