bonito_pci.c revision 1.3 1 /* $NetBSD: bonito_pci.c,v 1.3 2003/07/15 02:43:35 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * PCI configuration space support for the Algorithmics BONITO
41 * MIPS PCI and memory controller.
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: bonito_pci.c,v 1.3 2003/07/15 02:43:35 lukem Exp $");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/device.h>
50
51 #include <machine/bus.h>
52 #include <machine/intr.h>
53 #include <machine/locore.h>
54
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
57
58 #include <mips/bonito/bonitoreg.h>
59 #include <mips/bonito/bonitovar.h>
60
61 /*
62 * Bonito systems are always single-processor, so this is sufficient.
63 */
64 #define PCI_CONF_LOCK(s) (s) = splhigh()
65 #define PCI_CONF_UNLOCK(s) splx((s))
66
67 void bonito_attach_hook(struct device *, struct device *,
68 struct pcibus_attach_args *);
69 int bonito_bus_maxdevs(void *, int);
70 pcitag_t bonito_make_tag(void *, int, int, int);
71 void bonito_decompose_tag(void *, pcitag_t, int *, int *, int *);
72 pcireg_t bonito_conf_read(void *, pcitag_t, int);
73 void bonito_conf_write(void *, pcitag_t, int, pcireg_t);
74
75 void
76 bonito_pci_init(pci_chipset_tag_t pc, struct bonito_config *bc)
77 {
78
79 pc->pc_conf_v = bc;
80 pc->pc_attach_hook = bonito_attach_hook;
81 pc->pc_bus_maxdevs = bonito_bus_maxdevs;
82 pc->pc_make_tag = bonito_make_tag;
83 pc->pc_decompose_tag = bonito_decompose_tag;
84 pc->pc_conf_read = bonito_conf_read;
85 pc->pc_conf_write = bonito_conf_write;
86 }
87
88 void
89 bonito_attach_hook(struct device *parent, struct device *self,
90 struct pcibus_attach_args *pba)
91 {
92 }
93
94 int
95 bonito_bus_maxdevs(void *v, int busno)
96 {
97
98 return (32);
99 }
100
101 pcitag_t
102 bonito_make_tag(void *v, int b, int d, int f)
103 {
104
105 return ((b << 16) | (d << 11) | (f << 8));
106 }
107
108 void
109 bonito_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
110 {
111
112 if (bp != NULL)
113 *bp = (tag >> 16) & 0xff;
114 if (dp != NULL)
115 *dp = (tag >> 11) & 0x1f;
116 if (fp != NULL)
117 *fp = (tag >> 8) & 0x7;
118 }
119
120 static int
121 bonito_conf_addr(struct bonito_config *bc, pcitag_t tag, int offset,
122 u_int32_t *cfgoff, u_int32_t *pcimap_cfg)
123 {
124 int b, d, f;
125
126 bonito_decompose_tag(bc, tag, &b, &d, &f);
127
128 if (b == 0) {
129 if (d > (31 - bc->bc_adbase))
130 return (1);
131 *cfgoff = (1UL << (d + bc->bc_adbase)) | (f << 8) |
132 offset;
133 *pcimap_cfg = 0;
134 } else {
135 *cfgoff = tag | offset;
136 *pcimap_cfg = BONITO_PCIMAPCFG_TYPE1;
137 }
138
139 return (0);
140 }
141
142 pcireg_t
143 bonito_conf_read(void *v, pcitag_t tag, int offset)
144 {
145 struct bonito_config *bc = v;
146 pcireg_t data;
147 u_int32_t cfgoff, dummy, pcimap_cfg;
148 int s;
149
150 if (bonito_conf_addr(bc, tag, offset, &cfgoff, &pcimap_cfg))
151 return ((pcireg_t) -1);
152
153 PCI_CONF_LOCK(s);
154
155 /* clear aborts */
156 REGVAL(BONITO_PCICMD) |=
157 PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
158
159 /* high 16 bits of address go into PciMapCfg register */
160 REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
161
162 wbflush();
163 /* Issue a read to make sure the write is posted */
164 dummy = REGVAL(BONITO_PCIMAP_CFG);
165
166 /* low 16 bits of address are offset into config space */
167 data = REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc));
168
169 /* check for error */
170 if (REGVAL(BONITO_PCICMD) &
171 (PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT))
172 data = (pcireg_t) -1;
173
174 PCI_CONF_UNLOCK(s);
175
176 return (data);
177 }
178
179 void
180 bonito_conf_write(void *v, pcitag_t tag, int offset, pcireg_t data)
181 {
182 struct bonito_config *vt = v;
183 u_int32_t cfgoff, dummy, pcimap_cfg;
184 int s;
185
186 if (bonito_conf_addr(vt, tag, offset, &cfgoff, &pcimap_cfg))
187 panic("bonito_conf_write");
188
189 PCI_CONF_LOCK(s);
190
191 /* clear aborts */
192 REGVAL(BONITO_PCICMD) |=
193 PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
194
195 /* high 16 bits of address go into PciMapCfg register */
196 REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
197
198 wbflush();
199 /* Issue a read to make sure the write is posted */
200 dummy = REGVAL(BONITO_PCIMAP_CFG);
201
202 /* low 16 bits of address are offset into config space */
203 REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc)) = data;
204
205 PCI_CONF_UNLOCK(s);
206 }
207