xref: /openbsd-src/sys/arch/alpha/pci/lca_pci.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: lca_pci.c,v 1.10 2010/12/04 17:06:31 miod Exp $	*/
2 /* $NetBSD: lca_pci.c,v 1.13 1997/09/02 13:19:35 thorpej Exp $ */
3 
4 /*
5  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
6  * All rights reserved.
7  *
8  * Author: Chris G. Demetriou
9  *
10  * Permission to use, copy, modify and distribute this software and
11  * its documentation is hereby granted, provided that both the copyright
12  * notice and this permission notice appear in all copies of the
13  * software, derivative works or modified versions, and any portions
14  * thereof, and that both notices appear in supporting documentation.
15  *
16  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19  *
20  * Carnegie Mellon requests users of this software to return to
21  *
22  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
23  *  School of Computer Science
24  *  Carnegie Mellon University
25  *  Pittsburgh PA 15213-3890
26  *
27  * any improvements or extensions that they make and grant Carnegie the
28  * rights to redistribute these changes.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/device.h>
35 
36 #include <uvm/uvm_extern.h>
37 
38 #include <machine/autoconf.h>	/* badaddr proto */
39 
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
42 #include <alpha/pci/lcareg.h>
43 #include <alpha/pci/lcavar.h>
44 
45 void		lca_attach_hook(struct device *, struct device *,
46 		    struct pcibus_attach_args *);
47 int		lca_bus_maxdevs(void *, int);
48 pcitag_t	lca_make_tag(void *, int, int, int);
49 void		lca_decompose_tag(void *, pcitag_t, int *, int *,
50 		    int *);
51 int		lca_conf_size(void *, pcitag_t);
52 pcireg_t	lca_conf_read(void *, pcitag_t, int);
53 void		lca_conf_write(void *, pcitag_t, int, pcireg_t);
54 
55 void
56 lca_pci_init(pc, v)
57 	pci_chipset_tag_t pc;
58 	void *v;
59 {
60 
61 	pc->pc_conf_v = v;
62 	pc->pc_attach_hook = lca_attach_hook;
63 	pc->pc_bus_maxdevs = lca_bus_maxdevs;
64 	pc->pc_make_tag = lca_make_tag;
65 	pc->pc_decompose_tag = lca_decompose_tag;
66 	pc->pc_conf_size = lca_conf_size;
67 	pc->pc_conf_read = lca_conf_read;
68 	pc->pc_conf_write = lca_conf_write;
69 }
70 
71 void
72 lca_attach_hook(parent, self, pba)
73 	struct device *parent, *self;
74 	struct pcibus_attach_args *pba;
75 {
76 }
77 
78 int
79 lca_bus_maxdevs(cpv, busno)
80 	void *cpv;
81 	int busno;
82 {
83 
84 	if (busno == 0)
85 		return 16;
86 	else
87 		return 32;
88 }
89 
90 pcitag_t
91 lca_make_tag(cpv, b, d, f)
92 	void *cpv;
93 	int b, d, f;
94 {
95 
96 	return (b << 16) | (d << 11) | (f << 8);
97 }
98 
99 void
100 lca_decompose_tag(cpv, tag, bp, dp, fp)
101 	void *cpv;
102 	pcitag_t tag;
103 	int *bp, *dp, *fp;
104 {
105 
106 	if (bp != NULL)
107 		*bp = (tag >> 16) & 0xff;
108 	if (dp != NULL)
109 		*dp = (tag >> 11) & 0x1f;
110 	if (fp != NULL)
111 		*fp = (tag >> 8) & 0x7;
112 }
113 
114 int
115 lca_conf_size(void *cpv, pcitag_t tag)
116 {
117 	return PCI_CONFIG_SPACE_SIZE;
118 }
119 
120 pcireg_t
121 lca_conf_read(cpv, tag, offset)
122 	void *cpv;
123 	pcitag_t tag;
124 	int offset;
125 {
126 	struct lca_config *lcp = cpv;
127 	pcireg_t *datap, data;
128 	int s, secondary, device, ba;
129 
130 	s = 0;					/* XXX gcc -Wuninitialized */
131 
132 	/* secondary if bus # != 0 */
133 	pci_decompose_tag(&lcp->lc_pc, tag, &secondary, &device, 0);
134 	if (secondary) {
135 		s = splhigh();
136 		alpha_mb();
137 		REGVAL(LCA_IOC_CONF) = 0x01;
138 		alpha_mb();
139 	} else {
140 		/*
141 		 * on the LCA, must frob the tag used for
142 		 * devices on the primary bus, in the same ways
143 		 * as is used by type 1 configuration cycles
144 		 * on PCs.
145 		 */
146 		tag = (1 << (device + 11)) | (tag & 0x7ff);
147 	}
148 
149 	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(LCA_PCI_CONF |
150 	    tag << 5UL |					/* XXX */
151 	    (offset & ~0x03) << 5 |				/* XXX */
152 	    0 << 5 |						/* XXX */
153 	    0x3 << 3);						/* XXX */
154 	data = (pcireg_t)-1;
155 	if (!(ba = badaddr(datap, sizeof *datap)))
156 		data = *datap;
157 
158 	if (secondary) {
159 		alpha_mb();
160 		REGVAL(LCA_IOC_CONF) = 0x00;
161 		alpha_mb();
162 		splx(s);
163 	}
164 
165 #if 0
166 	printf("lca_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg,
167 	    data, datap, ba ? " (badaddr)" : "");
168 #endif
169 
170 	return data;
171 }
172 
173 void
174 lca_conf_write(cpv, tag, offset, data)
175 	void *cpv;
176 	pcitag_t tag;
177 	int offset;
178 	pcireg_t data;
179 {
180 	struct lca_config *lcp = cpv;
181 	pcireg_t *datap;
182 	int s, secondary, device;
183 
184 	s = 0;					/* XXX gcc -Wuninitialized */
185 
186 	/* secondary if bus # != 0 */
187 	pci_decompose_tag(&lcp->lc_pc, tag, &secondary, &device, 0);
188 	if (secondary) {
189 		s = splhigh();
190 		alpha_mb();
191 		REGVAL(LCA_IOC_CONF) = 0x01;
192 		alpha_mb();
193 	} else {
194 		/*
195 		 * on the LCA, must frob the tag used for
196 		 * devices on the primary bus, in the same ways
197 		 * as is used by type 1 configuration cycles
198 		 * on PCs.
199 		 */
200 		tag = (1 << (device + 11)) | (tag & 0x7ff);
201 	}
202 
203 	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(LCA_PCI_CONF |
204 	    tag << 5UL |					/* XXX */
205 	    (offset & ~0x03) << 5 |				/* XXX */
206 	    0 << 5 |						/* XXX */
207 	    0x3 << 3);						/* XXX */
208 	*datap = data;
209 
210 	if (secondary) {
211 		alpha_mb();
212 		REGVAL(LCA_IOC_CONF) = 0x00;
213 		alpha_mb();
214 		splx(s);
215 	}
216 
217 #if 0
218 	printf("lca_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
219 	    reg, data, datap);
220 #endif
221 }
222