1*adde9fa4Smiod /* $OpenBSD: apecs_pci.c,v 1.12 2015/10/30 07:51:49 miod Exp $ */
23a630e3fSniklas /* $NetBSD: apecs_pci.c,v 1.10 1996/11/13 21:13:25 cgd Exp $ */
3df930be7Sderaadt
4df930be7Sderaadt /*
5417eba8cSderaadt * Copyright (c) 1995, 1996 Carnegie-Mellon University.
6df930be7Sderaadt * All rights reserved.
7df930be7Sderaadt *
8df930be7Sderaadt * Author: Chris G. Demetriou
9df930be7Sderaadt *
10df930be7Sderaadt * Permission to use, copy, modify and distribute this software and
11df930be7Sderaadt * its documentation is hereby granted, provided that both the copyright
12df930be7Sderaadt * notice and this permission notice appear in all copies of the
13df930be7Sderaadt * software, derivative works or modified versions, and any portions
14df930be7Sderaadt * thereof, and that both notices appear in supporting documentation.
15df930be7Sderaadt *
16df930be7Sderaadt * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17df930be7Sderaadt * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18df930be7Sderaadt * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19df930be7Sderaadt *
20df930be7Sderaadt * Carnegie Mellon requests users of this software to return to
21df930be7Sderaadt *
22df930be7Sderaadt * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
23df930be7Sderaadt * School of Computer Science
24df930be7Sderaadt * Carnegie Mellon University
25df930be7Sderaadt * Pittsburgh PA 15213-3890
26df930be7Sderaadt *
27df930be7Sderaadt * any improvements or extensions that they make and grant Carnegie the
28df930be7Sderaadt * rights to redistribute these changes.
29df930be7Sderaadt */
30df930be7Sderaadt
31df930be7Sderaadt #include <sys/param.h>
32df930be7Sderaadt #include <sys/systm.h>
33df930be7Sderaadt #include <sys/kernel.h>
34df930be7Sderaadt #include <sys/device.h>
35625a31e9Sjason
36489e49f9Smiod #include <uvm/uvm_extern.h>
37df930be7Sderaadt
3850ce9ee0Sniklas #include <machine/autoconf.h> /* badaddr() proto */
3950ce9ee0Sniklas
40df930be7Sderaadt #include <dev/pci/pcireg.h>
41df930be7Sderaadt #include <dev/pci/pcivar.h>
42df930be7Sderaadt #include <alpha/pci/apecsreg.h>
4334fbf6deSderaadt #include <alpha/pci/apecsvar.h>
44df930be7Sderaadt
45c4071fd1Smillert void apecs_attach_hook(struct device *, struct device *,
46c4071fd1Smillert struct pcibus_attach_args *);
47c4071fd1Smillert int apecs_bus_maxdevs(void *, int);
48c4071fd1Smillert pcitag_t apecs_make_tag(void *, int, int, int);
49c4071fd1Smillert void apecs_decompose_tag(void *, pcitag_t, int *, int *,
50c4071fd1Smillert int *);
51b1926db3Smiod int apecs_conf_size(void *, pcitag_t);
52c4071fd1Smillert pcireg_t apecs_conf_read(void *, pcitag_t, int);
53c4071fd1Smillert void apecs_conf_write(void *, pcitag_t, int, pcireg_t);
54df930be7Sderaadt
55417eba8cSderaadt void
apecs_pci_init(pc,v)56417eba8cSderaadt apecs_pci_init(pc, v)
57417eba8cSderaadt pci_chipset_tag_t pc;
58417eba8cSderaadt void *v;
59417eba8cSderaadt {
60df930be7Sderaadt
61417eba8cSderaadt pc->pc_conf_v = v;
62417eba8cSderaadt pc->pc_attach_hook = apecs_attach_hook;
63417eba8cSderaadt pc->pc_bus_maxdevs = apecs_bus_maxdevs;
64417eba8cSderaadt pc->pc_make_tag = apecs_make_tag;
65417eba8cSderaadt pc->pc_decompose_tag = apecs_decompose_tag;
66b1926db3Smiod pc->pc_conf_size = apecs_conf_size;
67417eba8cSderaadt pc->pc_conf_read = apecs_conf_read;
68417eba8cSderaadt pc->pc_conf_write = apecs_conf_write;
69417eba8cSderaadt }
70417eba8cSderaadt
71417eba8cSderaadt void
apecs_attach_hook(parent,self,pba)72417eba8cSderaadt apecs_attach_hook(parent, self, pba)
73417eba8cSderaadt struct device *parent, *self;
74417eba8cSderaadt struct pcibus_attach_args *pba;
75417eba8cSderaadt {
76417eba8cSderaadt }
77417eba8cSderaadt
78417eba8cSderaadt int
apecs_bus_maxdevs(cpv,busno)79417eba8cSderaadt apecs_bus_maxdevs(cpv, busno)
80417eba8cSderaadt void *cpv;
81417eba8cSderaadt int busno;
82417eba8cSderaadt {
83417eba8cSderaadt
84417eba8cSderaadt return 32;
85417eba8cSderaadt }
86417eba8cSderaadt
87417eba8cSderaadt pcitag_t
apecs_make_tag(cpv,b,d,f)88417eba8cSderaadt apecs_make_tag(cpv, b, d, f)
89417eba8cSderaadt void *cpv;
90417eba8cSderaadt int b, d, f;
91417eba8cSderaadt {
92417eba8cSderaadt
93417eba8cSderaadt return (b << 16) | (d << 11) | (f << 8);
94417eba8cSderaadt }
95417eba8cSderaadt
96417eba8cSderaadt void
apecs_decompose_tag(cpv,tag,bp,dp,fp)97417eba8cSderaadt apecs_decompose_tag(cpv, tag, bp, dp, fp)
98417eba8cSderaadt void *cpv;
99417eba8cSderaadt pcitag_t tag;
100417eba8cSderaadt int *bp, *dp, *fp;
101417eba8cSderaadt {
102417eba8cSderaadt
103417eba8cSderaadt if (bp != NULL)
104417eba8cSderaadt *bp = (tag >> 16) & 0xff;
105417eba8cSderaadt if (dp != NULL)
106417eba8cSderaadt *dp = (tag >> 11) & 0x1f;
107417eba8cSderaadt if (fp != NULL)
108417eba8cSderaadt *fp = (tag >> 8) & 0x7;
109417eba8cSderaadt }
110417eba8cSderaadt
111b1926db3Smiod int
apecs_conf_size(void * cpv,pcitag_t tag)112b1926db3Smiod apecs_conf_size(void *cpv, pcitag_t tag)
113b1926db3Smiod {
114b1926db3Smiod return PCI_CONFIG_SPACE_SIZE;
115b1926db3Smiod }
116b1926db3Smiod
117417eba8cSderaadt pcireg_t
apecs_conf_read(cpv,tag,offset)11834fbf6deSderaadt apecs_conf_read(cpv, tag, offset)
11934fbf6deSderaadt void *cpv;
120417eba8cSderaadt pcitag_t tag;
121417eba8cSderaadt int offset;
122df930be7Sderaadt {
12334fbf6deSderaadt struct apecs_config *acp = cpv;
124417eba8cSderaadt pcireg_t *datap, data;
12534fbf6deSderaadt int s, secondary, ba;
12634fbf6deSderaadt int32_t old_haxr2; /* XXX */
127df930be7Sderaadt
1283a630e3fSniklas s = 0; /* XXX gcc -Wuninitialized */
1293a630e3fSniklas old_haxr2 = 0; /* XXX gcc -Wuninitialized */
1303a630e3fSniklas
131417eba8cSderaadt /* secondary if bus # != 0 */
132*adde9fa4Smiod pci_decompose_tag(&acp->ac_pc, tag, &secondary, NULL, NULL);
13334fbf6deSderaadt if (secondary) {
13434fbf6deSderaadt s = splhigh();
13534fbf6deSderaadt old_haxr2 = REGVAL(EPIC_HAXR2);
13650ce9ee0Sniklas alpha_mb();
13734fbf6deSderaadt REGVAL(EPIC_HAXR2) = old_haxr2 | 0x1;
13850ce9ee0Sniklas alpha_mb();
139df930be7Sderaadt }
140df930be7Sderaadt
14150ce9ee0Sniklas datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(APECS_PCI_CONF |
14234fbf6deSderaadt tag << 5UL | /* XXX */
14334fbf6deSderaadt (offset & ~0x03) << 5 | /* XXX */
14434fbf6deSderaadt 0 << 5 | /* XXX */
14534fbf6deSderaadt 0x3 << 3); /* XXX */
146417eba8cSderaadt data = (pcireg_t)-1;
14734fbf6deSderaadt if (!(ba = badaddr(datap, sizeof *datap)))
148df930be7Sderaadt data = *datap;
14934fbf6deSderaadt
15034fbf6deSderaadt if (secondary) {
15150ce9ee0Sniklas alpha_mb();
15234fbf6deSderaadt REGVAL(EPIC_HAXR2) = old_haxr2;
15350ce9ee0Sniklas alpha_mb();
15434fbf6deSderaadt splx(s);
15534fbf6deSderaadt }
15634fbf6deSderaadt
157df930be7Sderaadt #if 0
15834fbf6deSderaadt printf("apecs_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg,
15934fbf6deSderaadt data, datap, ba ? " (badaddr)" : "");
160df930be7Sderaadt #endif
16134fbf6deSderaadt
162df930be7Sderaadt return data;
163df930be7Sderaadt }
164df930be7Sderaadt
165df930be7Sderaadt void
apecs_conf_write(cpv,tag,offset,data)16634fbf6deSderaadt apecs_conf_write(cpv, tag, offset, data)
16734fbf6deSderaadt void *cpv;
168417eba8cSderaadt pcitag_t tag;
169417eba8cSderaadt int offset;
170417eba8cSderaadt pcireg_t data;
171df930be7Sderaadt {
17234fbf6deSderaadt struct apecs_config *acp = cpv;
173417eba8cSderaadt pcireg_t *datap;
17434fbf6deSderaadt int s, secondary;
17534fbf6deSderaadt int32_t old_haxr2; /* XXX */
176df930be7Sderaadt
1773a630e3fSniklas s = 0; /* XXX gcc -Wuninitialized */
1783a630e3fSniklas old_haxr2 = 0; /* XXX gcc -Wuninitialized */
1793a630e3fSniklas
180417eba8cSderaadt /* secondary if bus # != 0 */
181*adde9fa4Smiod pci_decompose_tag(&acp->ac_pc, tag, &secondary, NULL, NULL);
18234fbf6deSderaadt if (secondary) {
18334fbf6deSderaadt s = splhigh();
18434fbf6deSderaadt old_haxr2 = REGVAL(EPIC_HAXR2);
18550ce9ee0Sniklas alpha_mb();
18634fbf6deSderaadt REGVAL(EPIC_HAXR2) = old_haxr2 | 0x1;
18750ce9ee0Sniklas alpha_mb();
188df930be7Sderaadt }
189df930be7Sderaadt
19050ce9ee0Sniklas datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(APECS_PCI_CONF |
19134fbf6deSderaadt tag << 5UL | /* XXX */
19234fbf6deSderaadt (offset & ~0x03) << 5 | /* XXX */
19334fbf6deSderaadt 0 << 5 | /* XXX */
19434fbf6deSderaadt 0x3 << 3); /* XXX */
195625a31e9Sjason
196625a31e9Sjason alpha_mb();
19734fbf6deSderaadt *datap = data;
198625a31e9Sjason alpha_mb();
199625a31e9Sjason alpha_mb();
20034fbf6deSderaadt
20134fbf6deSderaadt if (secondary) {
20250ce9ee0Sniklas alpha_mb();
20334fbf6deSderaadt REGVAL(EPIC_HAXR2) = old_haxr2;
20450ce9ee0Sniklas alpha_mb();
20534fbf6deSderaadt splx(s);
20634fbf6deSderaadt }
20734fbf6deSderaadt
208df930be7Sderaadt #if 0
209df930be7Sderaadt printf("apecs_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
210df930be7Sderaadt reg, data, datap);
211df930be7Sderaadt #endif
212df930be7Sderaadt }
213