xref: /netbsd/sys/arch/mips/bonito/bonito_pci.c (revision c4a72b64)
1 /*	$NetBSD: bonito_pci.c,v 1.2 2002/08/18 15:57:55 simonb 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/param.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47 
48 #include <machine/bus.h>
49 #include <machine/intr.h>
50 #include <machine/locore.h>
51 
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
54 
55 #include <mips/bonito/bonitoreg.h>
56 #include <mips/bonito/bonitovar.h>
57 
58 /*
59  * Bonito systems are always single-processor, so this is sufficient.
60  */
61 #define	PCI_CONF_LOCK(s)	(s) = splhigh()
62 #define	PCI_CONF_UNLOCK(s)	splx((s))
63 
64 void		bonito_attach_hook(struct device *, struct device *,
65 		    struct pcibus_attach_args *);
66 int		bonito_bus_maxdevs(void *, int);
67 pcitag_t	bonito_make_tag(void *, int, int, int);
68 void		bonito_decompose_tag(void *, pcitag_t, int *, int *, int *);
69 pcireg_t	bonito_conf_read(void *, pcitag_t, int);
70 void		bonito_conf_write(void *, pcitag_t, int, pcireg_t);
71 
72 void
73 bonito_pci_init(pci_chipset_tag_t pc, struct bonito_config *bc)
74 {
75 
76 	pc->pc_conf_v = bc;
77 	pc->pc_attach_hook = bonito_attach_hook;
78 	pc->pc_bus_maxdevs = bonito_bus_maxdevs;
79 	pc->pc_make_tag = bonito_make_tag;
80 	pc->pc_decompose_tag = bonito_decompose_tag;
81 	pc->pc_conf_read = bonito_conf_read;
82 	pc->pc_conf_write = bonito_conf_write;
83 }
84 
85 void
86 bonito_attach_hook(struct device *parent, struct device *self,
87     struct pcibus_attach_args *pba)
88 {
89 }
90 
91 int
92 bonito_bus_maxdevs(void *v, int busno)
93 {
94 
95 	return (32);
96 }
97 
98 pcitag_t
99 bonito_make_tag(void *v, int b, int d, int f)
100 {
101 
102 	return ((b << 16) | (d << 11) | (f << 8));
103 }
104 
105 void
106 bonito_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
107 {
108 
109 	if (bp != NULL)
110 		*bp = (tag >> 16) & 0xff;
111 	if (dp != NULL)
112 		*dp = (tag >> 11) & 0x1f;
113 	if (fp != NULL)
114 		*fp = (tag >> 8) & 0x7;
115 }
116 
117 static int
118 bonito_conf_addr(struct bonito_config *bc, pcitag_t tag, int offset,
119     u_int32_t *cfgoff, u_int32_t *pcimap_cfg)
120 {
121 	int b, d, f;
122 
123 	bonito_decompose_tag(bc, tag, &b, &d, &f);
124 
125 	if (b == 0) {
126 		if (d > (31 - bc->bc_adbase))
127 			return (1);
128 		*cfgoff = (1UL << (d + bc->bc_adbase)) | (f << 8) |
129 		    offset;
130 		*pcimap_cfg = 0;
131 	} else {
132 		*cfgoff = tag | offset;
133 		*pcimap_cfg = BONITO_PCIMAPCFG_TYPE1;
134 	}
135 
136 	return (0);
137 }
138 
139 pcireg_t
140 bonito_conf_read(void *v, pcitag_t tag, int offset)
141 {
142 	struct bonito_config *bc = v;
143 	pcireg_t data;
144 	u_int32_t cfgoff, dummy, pcimap_cfg;
145 	int s;
146 
147 	if (bonito_conf_addr(bc, tag, offset, &cfgoff, &pcimap_cfg))
148 		return ((pcireg_t) -1);
149 
150 	PCI_CONF_LOCK(s);
151 
152 	/* clear aborts */
153 	REGVAL(BONITO_PCICMD) |=
154 	    PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
155 
156 	/* high 16 bits of address go into PciMapCfg register */
157 	REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
158 
159 	wbflush();
160 	/* Issue a read to make sure the write is posted */
161 	dummy = REGVAL(BONITO_PCIMAP_CFG);
162 
163 	/* low 16 bits of address are offset into config space */
164 	data = REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc));
165 
166 	/* check for error */
167 	if (REGVAL(BONITO_PCICMD) &
168 	    (PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT))
169 		data = (pcireg_t) -1;
170 
171 	PCI_CONF_UNLOCK(s);
172 
173 	return (data);
174 }
175 
176 void
177 bonito_conf_write(void *v, pcitag_t tag, int offset, pcireg_t data)
178 {
179 	struct bonito_config *vt = v;
180 	u_int32_t cfgoff, dummy, pcimap_cfg;
181 	int s;
182 
183 	if (bonito_conf_addr(vt, tag, offset, &cfgoff, &pcimap_cfg))
184 		panic("bonito_conf_write");
185 
186 	PCI_CONF_LOCK(s);
187 
188 	/* clear aborts */
189 	REGVAL(BONITO_PCICMD) |=
190 	    PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
191 
192 	/* high 16 bits of address go into PciMapCfg register */
193 	REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
194 
195 	wbflush();
196 	/* Issue a read to make sure the write is posted */
197 	dummy = REGVAL(BONITO_PCIMAP_CFG);
198 
199 	/* low 16 bits of address are offset into config space */
200 	REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc)) = data;
201 
202 	PCI_CONF_UNLOCK(s);
203 }
204