ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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         if ((busnum == 0) && (PCI_SLOT(devfn) == 0))
52                 return -1;
53
54         /* Clear status register bits. */
55         MSC_WRITE(MSC01_PCI_INTSTAT,
56                   (MSC01_PCI_INTCFG_MA_BIT | MSC01_PCI_INTCFG_TA_BIT));
57
58         /* Setup address */
59         if (busnum == 0)
60                 type = 0;       /* Type 0 */
61         else
62                 type = 1;       /* Type 1 */
63
64         MSC_WRITE(MSC01_PCI_CFGADDR,
65                   ((busnum << MSC01_PCI_CFGADDR_BNUM_SHF) |
66                    (PCI_SLOT(devfn) << MSC01_PCI_CFGADDR_DNUM_SHF)
67                    | (PCI_FUNC(devfn) <<
68                       MSC01_PCI_CFGADDR_FNUM_SHF) | ((where /
69                                                       4) <<
70                                                      MSC01_PCI_CFGADDR_RNUM_SHF)
71                    | (type)));
72
73         /* Perform access */
74         if (access_type == PCI_ACCESS_WRITE)
75                 MSC_WRITE(MSC01_PCI_CFGDATA, *data);
76         else
77                 MSC_READ(MSC01_PCI_CFGDATA, *data);
78
79         /* Detect Master/Target abort */
80         MSC_READ(MSC01_PCI_INTSTAT, intr);
81         if (intr & (MSC01_PCI_INTCFG_MA_BIT |
82                     MSC01_PCI_INTCFG_TA_BIT)) {
83                 /* Error occurred */
84
85                 /* Clear bits */
86                 MSC_READ(MSC01_PCI_INTSTAT, intr);
87                 MSC_WRITE(MSC01_PCI_INTSTAT,
88                           (MSC01_PCI_INTCFG_MA_BIT |
89                            MSC01_PCI_INTCFG_TA_BIT));
90
91                 return -1;
92         }
93
94         return 0;
95 }
96
97
98 /*
99  * We can't address 8 and 16 bit words directly.  Instead we have to
100  * read/write a 32bit word and mask/modify the data we actually want.
101  */
102 static int msc_pcibios_read(struct pci_bus *bus, unsigned int devfn,
103                              int where, int size, u32 * val)
104 {
105         u32 data = 0;
106
107         if ((size == 2) && (where & 1))
108                 return PCIBIOS_BAD_REGISTER_NUMBER;
109         else if ((size == 4) && (where & 3))
110                 return PCIBIOS_BAD_REGISTER_NUMBER;
111
112         if (msc_pcibios_config_access(PCI_ACCESS_READ, bus, devfn, where,
113                                       &data))
114                 return -1;
115
116         if (size == 1)
117                 *val = (data >> ((where & 3) << 3)) & 0xff;
118         else if (size == 2)
119                 *val = (data >> ((where & 3) << 3)) & 0xffff;
120         else
121                 *val = data;
122
123         return PCIBIOS_SUCCESSFUL;
124 }
125
126 static int msc_pcibios_write(struct pci_bus *bus, unsigned int devfn,
127                               int where, int size, u32 val)
128 {
129         u32 data = 0;
130
131         if ((size == 2) && (where & 1))
132                 return PCIBIOS_BAD_REGISTER_NUMBER;
133         else if ((size == 4) && (where & 3))
134                 return PCIBIOS_BAD_REGISTER_NUMBER;
135
136         if (size == 4)
137                 data = val;
138         else {
139                 if (msc_pcibios_config_access(PCI_ACCESS_READ, bus, devfn,
140                                               where, &data))
141                         return -1;
142
143                 if (size == 1)
144                         data = (data & ~(0xff << ((where & 3) << 3))) |
145                                 (val << ((where & 3) << 3));
146                 else if (size == 2)
147                         data = (data & ~(0xffff << ((where & 3) << 3))) |
148                                 (val << ((where & 3) << 3));
149         }
150
151         if (msc_pcibios_config_access(PCI_ACCESS_WRITE, bus, devfn, where,
152                                        &data))
153                 return -1;
154
155         return PCIBIOS_SUCCESSFUL;
156 }
157
158 struct pci_ops msc_pci_ops = {
159         .read = msc_pcibios_read,
160         .write = msc_pcibios_write
161 };