1/* $NetBSD: cia_pci.c,v 1.31 2011/06/14 15:34:22 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.31 2011/06/14 15:34:22 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
44void		cia_attach_hook(device_t, device_t,
45		    struct pcibus_attach_args *);
46int		cia_bus_maxdevs(void *, int);
47pcitag_t	cia_make_tag(void *, int, int, int);
48void		cia_decompose_tag(void *, pcitag_t, int *, int *, int *);
49pcireg_t	cia_conf_read(void *, pcitag_t, int);
50void		cia_conf_write(void *, pcitag_t, int, pcireg_t);
51
52void
53cia_pci_init(pci_chipset_tag_t pc, void *v)
54{
55
56	pc->pc_conf_v = v;
57	pc->pc_attach_hook = cia_attach_hook;
58	pc->pc_bus_maxdevs = cia_bus_maxdevs;
59	pc->pc_make_tag = cia_make_tag;
60	pc->pc_decompose_tag = cia_decompose_tag;
61	pc->pc_conf_read = cia_conf_read;
62	pc->pc_conf_write = cia_conf_write;
63}
64
65void
66cia_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
67{
68}
69
70int
71cia_bus_maxdevs(void *cpv, int busno)
72{
73
74	return 32;
75}
76
77pcitag_t
78cia_make_tag(void *cpv, int b, int d, int f)
79{
80
81	return (b << 16) | (d << 11) | (f << 8);
82}
83
84void
85cia_decompose_tag(void *cpv, pcitag_t tag, int *bp, int *dp, int *fp)
86{
87
88	if (bp != NULL)
89		*bp = (tag >> 16) & 0xff;
90	if (dp != NULL)
91		*dp = (tag >> 11) & 0x1f;
92	if (fp != NULL)
93		*fp = (tag >> 8) & 0x7;
94}
95
96pcireg_t
97cia_conf_read(void *cpv, pcitag_t tag, int offset)
98{
99	struct cia_config *ccp = cpv;
100	pcireg_t *datap, data;
101	int s, secondary, ba;
102	uint32_t old_cfg, errbits;
103
104#ifdef __GNUC__
105	s = 0;					/* XXX gcc -Wuninitialized */
106	old_cfg = 0;				/* XXX gcc -Wuninitialized */
107#endif
108
109	/*
110	 * Some (apparently-common) revisions of EB164 and AlphaStation
111	 * firmware do the Wrong thing with PCI master and target aborts,
112	 * which are caused by accesing the configuration space of devices
113	 * that don't exist (for example).
114	 *
115	 * To work around this, we clear the CIA error register's PCI
116	 * master and target abort bits before touching PCI configuration
117	 * space and check it afterwards.  If it indicates a master or target
118	 * abort, the device wasn't there so we return 0xffffffff.
119	 */
120	REGVAL(CIA_CSR_CIA_ERR) = CIA_ERR_RCVD_MAS_ABT|CIA_ERR_RCVD_TAR_ABT;
121	alpha_mb();
122	alpha_pal_draina();
123
124	/* secondary if bus # != 0 */
125	pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
126	if (secondary) {
127		s = splhigh();
128		old_cfg = REGVAL(CIA_CSR_CFG);
129		alpha_mb();
130		REGVAL(CIA_CSR_CFG) = old_cfg | 0x1;
131		alpha_mb();
132	}
133
134	/*
135	 * We just inline the BWX support, since this is the only
136	 * difference between BWX and swiz for config space.
137	 */
138	if (ccp->cc_flags & CCF_PCI_USE_BWX) {
139		if (secondary) {
140			datap =
141			    (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF1 |
142				tag | (offset & ~0x03));
143		} else {
144			datap =
145			    (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF0 |
146				tag | (offset & ~0x03));
147		}
148	} else {
149		datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
150		    tag << 5UL |				/* XXX */
151		    (offset & ~0x03) << 5 |			/* XXX */
152		    0 << 5 |					/* XXX */
153		    0x3 << 3);					/* XXX */
154	}
155	data = (pcireg_t)-1;
156	alpha_mb();
157	if (!(ba = badaddr(datap, sizeof *datap)))
158		data = *datap;
159	alpha_mb();
160	alpha_mb();
161
162	if (secondary) {
163		alpha_mb();
164		REGVAL(CIA_CSR_CFG) = old_cfg;
165		alpha_mb();
166		splx(s);
167	}
168
169	alpha_pal_draina();
170	alpha_mb();
171	errbits = REGVAL(CIA_CSR_CIA_ERR);
172	if (errbits & (CIA_ERR_RCVD_MAS_ABT|CIA_ERR_RCVD_TAR_ABT)) {
173		ba = 1;
174		data = 0xffffffff;
175	}
176
177	if (errbits) {
178		REGVAL(CIA_CSR_CIA_ERR) = errbits;
179		alpha_mb();
180		alpha_pal_draina();
181	}
182
183#if 0
184	printf("cia_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg,
185	    data, datap, ba ? " (badaddr)" : "");
186#endif
187
188	return data;
189}
190
191void
192cia_conf_write(void *cpv, pcitag_t tag, int offset, pcireg_t data)
193{
194	struct cia_config *ccp = cpv;
195	pcireg_t *datap;
196	int s, secondary;
197	uint32_t old_cfg;
198
199#ifdef __GNUC__
200	s = 0;					/* XXX gcc -Wuninitialized */
201	old_cfg = 0;				/* XXX gcc -Wuninitialized */
202#endif
203
204	/* secondary if bus # != 0 */
205	pci_decompose_tag(&ccp->cc_pc, tag, &secondary, 0, 0);
206	if (secondary) {
207		s = splhigh();
208		old_cfg = REGVAL(CIA_CSR_CFG);
209		alpha_mb();
210		REGVAL(CIA_CSR_CFG) = old_cfg | 0x1;
211		alpha_mb();
212	}
213
214	/*
215	 * We just inline the BWX support, since this is the only
216	 * difference between BWX and swiz for config space.
217	 */
218	if (ccp->cc_flags & CCF_PCI_USE_BWX) {
219		if (secondary) {
220			datap =
221			    (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF1 |
222				tag | (offset & ~0x03));
223		} else {
224			datap =
225			    (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_EV56_BWCONF0 |
226				tag | (offset & ~0x03));
227		}
228	} else {
229		datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(CIA_PCI_CONF |
230		    tag << 5UL |				/* XXX */
231		    (offset & ~0x03) << 5 |			/* XXX */
232		    0 << 5 |					/* XXX */
233		    0x3 << 3);					/* XXX */
234	}
235	alpha_mb();
236	*datap = data;
237	alpha_mb();
238	alpha_mb();
239
240	if (secondary) {
241		alpha_mb();
242		REGVAL(CIA_CSR_CFG) = old_cfg;
243		alpha_mb();
244		splx(s);
245	}
246
247#if 0
248	printf("cia_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
249	    reg, data, datap);
250#endif
251}
252