xref: /netbsd/sys/arch/alpha/pci/cia_pci.c (revision 6550d01e)
1 /* $NetBSD: cia_pci.c,v 1.30 2010/12/15 01:27:18 matt Exp $ */
2 
3 /*
4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5  * All rights reserved.
6  *
7  * Author: Chris G. Demetriou
8  *
9  * Permission to use, copy, modify and distribute this software and
10  * its documentation is hereby granted, provided that both the copyright
11  * notice and this permission notice appear in all copies of the
12  * software, derivative works or modified versions, and any portions
13  * thereof, and that both notices appear in supporting documentation.
14  *
15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie the
27  * rights to redistribute these changes.
28  */
29 
30 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
31 
32 __KERNEL_RCSID(0, "$NetBSD: cia_pci.c,v 1.30 2010/12/15 01:27:18 matt Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 
39 #include <dev/pci/pcireg.h>
40 #include <dev/pci/pcivar.h>
41 #include <alpha/pci/ciareg.h>
42 #include <alpha/pci/ciavar.h>
43 
44 void		cia_attach_hook(struct device *, struct device *,
45 		    struct pcibus_attach_args *);
46 int		cia_bus_maxdevs(void *, int);
47 pcitag_t	cia_make_tag(void *, int, int, int);
48 void		cia_decompose_tag(void *, pcitag_t, int *, int *,
49 		    int *);
50 pcireg_t	cia_conf_read(void *, pcitag_t, int);
51 void		cia_conf_write(void *, pcitag_t, int, pcireg_t);
52 
53 void
54 cia_pci_init(pci_chipset_tag_t pc, void *v)
55 {
56 
57 	pc->pc_conf_v = v;
58 	pc->pc_attach_hook = cia_attach_hook;
59 	pc->pc_bus_maxdevs = cia_bus_maxdevs;
60 	pc->pc_make_tag = cia_make_tag;
61 	pc->pc_decompose_tag = cia_decompose_tag;
62 	pc->pc_conf_read = cia_conf_read;
63 	pc->pc_conf_write = cia_conf_write;
64 }
65 
66 void
67 cia_attach_hook(struct device *parent, struct device *self, struct pcibus_attach_args *pba)
68 {
69 }
70 
71 int
72 cia_bus_maxdevs(void *cpv, int busno)
73 {
74 
75 	return 32;
76 }
77 
78 pcitag_t
79 cia_make_tag(void *cpv, int b, int d, int f)
80 {
81 
82 	return (b << 16) | (d << 11) | (f << 8);
83 }
84 
85 void
86 cia_decompose_tag(void *cpv, pcitag_t tag, int *bp, int *dp, int *fp)
87 {
88 
89 	if (bp != NULL)
90 		*bp = (tag >> 16) & 0xff;
91 	if (dp != NULL)
92 		*dp = (tag >> 11) & 0x1f;
93 	if (fp != NULL)
94 		*fp = (tag >> 8) & 0x7;
95 }
96 
97 pcireg_t
98 cia_conf_read(void *cpv, pcitag_t tag, int offset)
99 {
100 	struct cia_config *ccp = cpv;
101 	pcireg_t *datap, data;
102 	int s, secondary, ba;
103 	u_int32_t old_cfg, errbits;
104 
105 #ifdef __GNUC__
106 	s = 0;					/* XXX gcc -Wuninitialized */
107 	old_cfg = 0;				/* XXX gcc -Wuninitialized */
108 #endif
109 
110 	/*
111 	 * Some (apparently-common) revisions of EB164 and AlphaStation
112 	 * firmware do the Wrong thing with PCI master and target aborts,
113 	 * which are caused by accesing the configuration space of devices
114 	 * that don't exist (for example).
115 	 *
116 	 * To work around this, we clear the CIA error register's PCI
117 	 * master and target abort bits before touching PCI configuration
118 	 * space and check it afterwards.  If it indicates a master or target
119 	 * abort, the device wasn't there so we return 0xffffffff.
120 	 */
121 	REGVAL(CIA_CSR_CIA_ERR) = CIA_ERR_RCVD_MAS_ABT|CIA_ERR_RCVD_TAR_ABT;
122 	alpha_mb();
123 	alpha_pal_draina();
124 
125 	/* secondary if bus # != 0 */
126 	pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
127 	if (secondary) {
128 		s = splhigh();
129 		old_cfg = REGVAL(CIA_CSR_CFG);
130 		alpha_mb();
131 		REGVAL(CIA_CSR_CFG) = old_cfg | 0x1;
132 		alpha_mb();
133 	}
134 
135 	/*
136 	 * We just inline the BWX support, since this is the only
137 	 * difference between BWX and swiz for config space.
138 	 */
139 	if (ccp->cc_flags & CCF_PCI_USE_BWX) {
140 		if (secondary) {
141 			datap =
142 			    (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF1 |
143 				tag | (offset & ~0x03));
144 		} else {
145 			datap =
146 			    (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF0 |
147 				tag | (offset & ~0x03));
148 		}
149 	} else {
150 		datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
151 		    tag << 5UL |				/* XXX */
152 		    (offset & ~0x03) << 5 |			/* XXX */
153 		    0 << 5 |					/* XXX */
154 		    0x3 << 3);					/* XXX */
155 	}
156 	data = (pcireg_t)-1;
157 	alpha_mb();
158 	if (!(ba = badaddr(datap, sizeof *datap)))
159 		data = *datap;
160 	alpha_mb();
161 	alpha_mb();
162 
163 	if (secondary) {
164 		alpha_mb();
165 		REGVAL(CIA_CSR_CFG) = old_cfg;
166 		alpha_mb();
167 		splx(s);
168 	}
169 
170 	alpha_pal_draina();
171 	alpha_mb();
172 	errbits = REGVAL(CIA_CSR_CIA_ERR);
173 	if (errbits & (CIA_ERR_RCVD_MAS_ABT|CIA_ERR_RCVD_TAR_ABT)) {
174 		ba = 1;
175 		data = 0xffffffff;
176 	}
177 
178 	if (errbits) {
179 		REGVAL(CIA_CSR_CIA_ERR) = errbits;
180 		alpha_mb();
181 		alpha_pal_draina();
182 	}
183 
184 #if 0
185 	printf("cia_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg,
186 	    data, datap, ba ? " (badaddr)" : "");
187 #endif
188 
189 	return data;
190 }
191 
192 void
193 cia_conf_write(void *cpv, pcitag_t tag, int offset, pcireg_t data)
194 {
195 	struct cia_config *ccp = cpv;
196 	pcireg_t *datap;
197 	int s, secondary;
198 	u_int32_t old_cfg;
199 
200 #ifdef __GNUC__
201 	s = 0;					/* XXX gcc -Wuninitialized */
202 	old_cfg = 0;				/* XXX gcc -Wuninitialized */
203 #endif
204 
205 	/* secondary if bus # != 0 */
206 	pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
207 	if (secondary) {
208 		s = splhigh();
209 		old_cfg = REGVAL(CIA_CSR_CFG);
210 		alpha_mb();
211 		REGVAL(CIA_CSR_CFG) = old_cfg | 0x1;
212 		alpha_mb();
213 	}
214 
215 	/*
216 	 * We just inline the BWX support, since this is the only
217 	 * difference between BWX and swiz for config space.
218 	 */
219 	if (ccp->cc_flags & CCF_PCI_USE_BWX) {
220 		if (secondary) {
221 			datap =
222 			    (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF1 |
223 				tag | (offset & ~0x03));
224 		} else {
225 			datap =
226 			    (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF0 |
227 				tag | (offset & ~0x03));
228 		}
229 	} else {
230 		datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
231 		    tag << 5UL |				/* XXX */
232 		    (offset & ~0x03) << 5 |			/* XXX */
233 		    0 << 5 |					/* XXX */
234 		    0x3 << 3);					/* XXX */
235 	}
236 	alpha_mb();
237 	*datap = data;
238 	alpha_mb();
239 	alpha_mb();
240 
241 	if (secondary) {
242 		alpha_mb();
243 		REGVAL(CIA_CSR_CFG) = old_cfg;
244 		alpha_mb();
245 		splx(s);
246 	}
247 
248 #if 0
249 	printf("cia_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
250 	    reg, data, datap);
251 #endif
252 }
253