1/*	$OpenBSD: apecs_pci.c,v 1.12 2015/10/30 07:51:49 miod Exp $	*/
2/*	$NetBSD: apecs_pci.c,v 1.10 1996/11/13 21:13:25 cgd 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/apecsreg.h>
43#include <alpha/pci/apecsvar.h>
44
45void		apecs_attach_hook(struct device *, struct device *,
46		    struct pcibus_attach_args *);
47int		apecs_bus_maxdevs(void *, int);
48pcitag_t	apecs_make_tag(void *, int, int, int);
49void		apecs_decompose_tag(void *, pcitag_t, int *, int *,
50		    int *);
51int		apecs_conf_size(void *, pcitag_t);
52pcireg_t	apecs_conf_read(void *, pcitag_t, int);
53void		apecs_conf_write(void *, pcitag_t, int, pcireg_t);
54
55void
56apecs_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 = apecs_attach_hook;
63	pc->pc_bus_maxdevs = apecs_bus_maxdevs;
64	pc->pc_make_tag = apecs_make_tag;
65	pc->pc_decompose_tag = apecs_decompose_tag;
66	pc->pc_conf_size = apecs_conf_size;
67	pc->pc_conf_read = apecs_conf_read;
68	pc->pc_conf_write = apecs_conf_write;
69}
70
71void
72apecs_attach_hook(parent, self, pba)
73	struct device *parent, *self;
74	struct pcibus_attach_args *pba;
75{
76}
77
78int
79apecs_bus_maxdevs(cpv, busno)
80	void *cpv;
81	int busno;
82{
83
84	return 32;
85}
86
87pcitag_t
88apecs_make_tag(cpv, b, d, f)
89	void *cpv;
90	int b, d, f;
91{
92
93	return (b << 16) | (d << 11) | (f << 8);
94}
95
96void
97apecs_decompose_tag(cpv, tag, bp, dp, fp)
98	void *cpv;
99	pcitag_t tag;
100	int *bp, *dp, *fp;
101{
102
103	if (bp != NULL)
104		*bp = (tag >> 16) & 0xff;
105	if (dp != NULL)
106		*dp = (tag >> 11) & 0x1f;
107	if (fp != NULL)
108		*fp = (tag >> 8) & 0x7;
109}
110
111int
112apecs_conf_size(void *cpv, pcitag_t tag)
113{
114	return PCI_CONFIG_SPACE_SIZE;
115}
116
117pcireg_t
118apecs_conf_read(cpv, tag, offset)
119	void *cpv;
120	pcitag_t tag;
121	int offset;
122{
123	struct apecs_config *acp = cpv;
124	pcireg_t *datap, data;
125	int s, secondary, ba;
126	int32_t old_haxr2;					/* XXX */
127
128	s = 0;					/* XXX gcc -Wuninitialized */
129	old_haxr2 = 0;				/* XXX gcc -Wuninitialized */
130
131	/* secondary if bus # != 0 */
132	pci_decompose_tag(&acp->ac_pc, tag, &secondary, NULL, NULL);
133	if (secondary) {
134		s = splhigh();
135		old_haxr2 = REGVAL(EPIC_HAXR2);
136		alpha_mb();
137		REGVAL(EPIC_HAXR2) = old_haxr2 | 0x1;
138		alpha_mb();
139	}
140
141	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(APECS_PCI_CONF |
142	    tag << 5UL |					/* XXX */
143	    (offset & ~0x03) << 5 |				/* XXX */
144	    0 << 5 |						/* XXX */
145	    0x3 << 3);						/* XXX */
146	data = (pcireg_t)-1;
147	if (!(ba = badaddr(datap, sizeof *datap)))
148		data = *datap;
149
150	if (secondary) {
151		alpha_mb();
152		REGVAL(EPIC_HAXR2) = old_haxr2;
153		alpha_mb();
154		splx(s);
155	}
156
157#if 0
158	printf("apecs_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg,
159	    data, datap, ba ? " (badaddr)" : "");
160#endif
161
162	return data;
163}
164
165void
166apecs_conf_write(cpv, tag, offset, data)
167	void *cpv;
168	pcitag_t tag;
169	int offset;
170	pcireg_t data;
171{
172	struct apecs_config *acp = cpv;
173	pcireg_t *datap;
174	int s, secondary;
175	int32_t old_haxr2;					/* XXX */
176
177	s = 0;					/* XXX gcc -Wuninitialized */
178	old_haxr2 = 0;				/* XXX gcc -Wuninitialized */
179
180	/* secondary if bus # != 0 */
181	pci_decompose_tag(&acp->ac_pc, tag, &secondary, NULL, NULL);
182	if (secondary) {
183		s = splhigh();
184		old_haxr2 = REGVAL(EPIC_HAXR2);
185		alpha_mb();
186		REGVAL(EPIC_HAXR2) = old_haxr2 | 0x1;
187		alpha_mb();
188	}
189
190	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(APECS_PCI_CONF |
191	    tag << 5UL |					/* XXX */
192	    (offset & ~0x03) << 5 |				/* XXX */
193	    0 << 5 |						/* XXX */
194	    0x3 << 3);						/* XXX */
195
196	alpha_mb();
197	*datap = data;
198	alpha_mb();
199	alpha_mb();
200
201	if (secondary) {
202		alpha_mb();
203		REGVAL(EPIC_HAXR2) = old_haxr2;
204		alpha_mb();
205		splx(s);
206	}
207
208#if 0
209	printf("apecs_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
210	    reg, data, datap);
211#endif
212}
213