xref: /netbsd/sys/arch/mips/bonito/bonito_pci.c (revision 6550d01e)
1 /*	$NetBSD: bonito_pci.c,v 1.7 2009/08/09 04:09:55 matt 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.7 2009/08/09 04:09:55 matt 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(device_t, device_t,
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(device_t parent, device_t 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 bool
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 true;
124 		*cfgoff = (1UL << (d + bc->bc_adbase)) | (f << 8) | offset;
125 		*pcimap_cfg = 0;
126 	} else {
127 		*cfgoff = tag | offset;
128 		*pcimap_cfg = BONITO_PCIMAPCFG_TYPE1;
129 	}
130 
131 	return false;
132 }
133 
134 pcireg_t
135 bonito_conf_read(void *v, pcitag_t tag, int offset)
136 {
137 	struct bonito_config *bc = v;
138 	pcireg_t data;
139 	u_int32_t cfgoff, dummy, pcimap_cfg;
140 	int s;
141 
142 	if (bonito_conf_addr(bc, tag, offset, &cfgoff, &pcimap_cfg))
143 		return ((pcireg_t) -1);
144 
145 	PCI_CONF_LOCK(s);
146 
147 	/* clear aborts */
148 	REGVAL(BONITO_PCICMD) |=
149 	    PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
150 
151 	/* high 16 bits of address go into PciMapCfg register */
152 	REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
153 
154 	wbflush();
155 	/* Issue a read to make sure the write is posted */
156 	dummy = REGVAL(BONITO_PCIMAP_CFG);
157 
158 	/* low 16 bits of address are offset into config space */
159 	data = REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc));
160 
161 	/* check for error */
162 	if (REGVAL(BONITO_PCICMD) &
163 	    (PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT))
164 		data = (pcireg_t) -1;
165 
166 	PCI_CONF_UNLOCK(s);
167 
168 	return (data);
169 }
170 
171 void
172 bonito_conf_write(void *v, pcitag_t tag, int offset, pcireg_t data)
173 {
174 	struct bonito_config *vt = v;
175 	u_int32_t cfgoff, dummy, pcimap_cfg;
176 	int s;
177 
178 	if (bonito_conf_addr(vt, tag, offset, &cfgoff, &pcimap_cfg))
179 		panic("bonito_conf_write");
180 
181 	PCI_CONF_LOCK(s);
182 
183 	/* clear aborts */
184 	REGVAL(BONITO_PCICMD) |=
185 	    PCI_STATUS_MASTER_ABORT | PCI_STATUS_MASTER_TARGET_ABORT;
186 
187 	/* high 16 bits of address go into PciMapCfg register */
188 	REGVAL(BONITO_PCIMAP_CFG) = (cfgoff >> 16) | pcimap_cfg;
189 
190 	wbflush();
191 	/* Issue a read to make sure the write is posted */
192 	dummy = REGVAL(BONITO_PCIMAP_CFG);
193 
194 	/* low 16 bits of address are offset into config space */
195 	REGVAL(BONITO_PCICFG_BASE + (cfgoff & 0xfffc)) = data;
196 
197 	PCI_CONF_UNLOCK(s);
198 }
199