xref: /netbsd-src/sys/arch/mips/bonito/bonito_pci.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: bonito_pci.c,v 1.5 2008/04/28 20:23:28 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * PCI configuration space support for the Algorithmics BONITO
34  * MIPS PCI and memory controller.
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: bonito_pci.c,v 1.5 2008/04/28 20:23:28 martin Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46 #include <machine/locore.h>
47 
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 
51 #include <mips/bonito/bonitoreg.h>
52 #include <mips/bonito/bonitovar.h>
53 
54 /*
55  * Bonito systems are always single-processor, so this is sufficient.
56  */
57 #define	PCI_CONF_LOCK(s)	(s) = splhigh()
58 #define	PCI_CONF_UNLOCK(s)	splx((s))
59 
60 void		bonito_attach_hook(struct device *, struct device *,
61 		    struct pcibus_attach_args *);
62 int		bonito_bus_maxdevs(void *, int);
63 pcitag_t	bonito_make_tag(void *, int, int, int);
64 void		bonito_decompose_tag(void *, pcitag_t, int *, int *, int *);
65 pcireg_t	bonito_conf_read(void *, pcitag_t, int);
66 void		bonito_conf_write(void *, pcitag_t, int, pcireg_t);
67 
68 void
69 bonito_pci_init(pci_chipset_tag_t pc, struct bonito_config *bc)
70 {
71 
72 	pc->pc_conf_v = bc;
73 	pc->pc_attach_hook = bonito_attach_hook;
74 	pc->pc_bus_maxdevs = bonito_bus_maxdevs;
75 	pc->pc_make_tag = bonito_make_tag;
76 	pc->pc_decompose_tag = bonito_decompose_tag;
77 	pc->pc_conf_read = bonito_conf_read;
78 	pc->pc_conf_write = bonito_conf_write;
79 }
80 
81 void
82 bonito_attach_hook(struct device *parent, struct device *self,
83     struct pcibus_attach_args *pba)
84 {
85 }
86 
87 int
88 bonito_bus_maxdevs(void *v, int busno)
89 {
90 
91 	return (32);
92 }
93 
94 pcitag_t
95 bonito_make_tag(void *v, int b, int d, int f)
96 {
97 
98 	return ((b << 16) | (d << 11) | (f << 8));
99 }
100 
101 void
102 bonito_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
103 {
104 
105 	if (bp != NULL)
106 		*bp = (tag >> 16) & 0xff;
107 	if (dp != NULL)
108 		*dp = (tag >> 11) & 0x1f;
109 	if (fp != NULL)
110 		*fp = (tag >> 8) & 0x7;
111 }
112 
113 static int
114 bonito_conf_addr(struct bonito_config *bc, pcitag_t tag, int offset,
115     u_int32_t *cfgoff, u_int32_t *pcimap_cfg)
116 {
117 	int b, d, f;
118 
119 	bonito_decompose_tag(bc, tag, &b, &d, &f);
120 
121 	if (b == 0) {
122 		if (d > (31 - bc->bc_adbase))
123 			return (1);
124 		*cfgoff = (1UL << (d + bc->bc_adbase)) | (f << 8) |
125 		    offset;
126 		*pcimap_cfg = 0;
127 	} else {
128 		*cfgoff = tag | offset;
129 		*pcimap_cfg = BONITO_PCIMAPCFG_TYPE1;
130 	}
131 
132 	return (0);
133 }
134 
135 pcireg_t
136 bonito_conf_read(void *v, pcitag_t tag, int offset)
137 {
138 	struct bonito_config *bc = v;
139 	pcireg_t data;
140 	u_int32_t cfgoff, dummy, pcimap_cfg;
141 	int s;
142 
143 	if (bonito_conf_addr(bc, tag, offset, &cfgoff, &pcimap_cfg))
144 		return ((pcireg_t) -1);
145 
146 	PCI_CONF_LOCK(s);
147 
148 	/* clear aborts */
149 	REGVAL(BONITO_PCICMD) |=
150 	    PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
151 
152 	/* high 16 bits of address go into PciMapCfg register */
153 	REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
154 
155 	wbflush();
156 	/* Issue a read to make sure the write is posted */
157 	dummy = REGVAL(BONITO_PCIMAP_CFG);
158 
159 	/* low 16 bits of address are offset into config space */
160 	data = REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc));
161 
162 	/* check for error */
163 	if (REGVAL(BONITO_PCICMD) &
164 	    (PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT))
165 		data = (pcireg_t) -1;
166 
167 	PCI_CONF_UNLOCK(s);
168 
169 	return (data);
170 }
171 
172 void
173 bonito_conf_write(void *v, pcitag_t tag, int offset, pcireg_t data)
174 {
175 	struct bonito_config *vt = v;
176 	u_int32_t cfgoff, dummy, pcimap_cfg;
177 	int s;
178 
179 	if (bonito_conf_addr(vt, tag, offset, &cfgoff, &pcimap_cfg))
180 		panic("bonito_conf_write");
181 
182 	PCI_CONF_LOCK(s);
183 
184 	/* clear aborts */
185 	REGVAL(BONITO_PCICMD) |=
186 	    PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
187 
188 	/* high 16 bits of address go into PciMapCfg register */
189 	REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
190 
191 	wbflush();
192 	/* Issue a read to make sure the write is posted */
193 	dummy = REGVAL(BONITO_PCIMAP_CFG);
194 
195 	/* low 16 bits of address are offset into config space */
196 	REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc)) = data;
197 
198 	PCI_CONF_UNLOCK(s);
199 }
200