1*adde9fa4Smiod /* $OpenBSD: lca_pci.c,v 1.12 2015/10/30 07:51:49 miod Exp $ */
216bde9efSjason /* $NetBSD: lca_pci.c,v 1.13 1997/09/02 13:19:35 thorpej Exp $ */
334fbf6deSderaadt
434fbf6deSderaadt /*
5417eba8cSderaadt * Copyright (c) 1995, 1996 Carnegie-Mellon University.
634fbf6deSderaadt * All rights reserved.
734fbf6deSderaadt *
8417eba8cSderaadt * Author: Chris G. Demetriou
934fbf6deSderaadt *
1034fbf6deSderaadt * Permission to use, copy, modify and distribute this software and
1134fbf6deSderaadt * its documentation is hereby granted, provided that both the copyright
1234fbf6deSderaadt * notice and this permission notice appear in all copies of the
1334fbf6deSderaadt * software, derivative works or modified versions, and any portions
1434fbf6deSderaadt * thereof, and that both notices appear in supporting documentation.
1534fbf6deSderaadt *
1634fbf6deSderaadt * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
1734fbf6deSderaadt * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
1834fbf6deSderaadt * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1934fbf6deSderaadt *
2034fbf6deSderaadt * Carnegie Mellon requests users of this software to return to
2134fbf6deSderaadt *
2234fbf6deSderaadt * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
2334fbf6deSderaadt * School of Computer Science
2434fbf6deSderaadt * Carnegie Mellon University
2534fbf6deSderaadt * Pittsburgh PA 15213-3890
2634fbf6deSderaadt *
2734fbf6deSderaadt * any improvements or extensions that they make and grant Carnegie the
2834fbf6deSderaadt * rights to redistribute these changes.
2934fbf6deSderaadt */
3034fbf6deSderaadt
3134fbf6deSderaadt #include <sys/param.h>
3234fbf6deSderaadt #include <sys/systm.h>
3334fbf6deSderaadt #include <sys/kernel.h>
3434fbf6deSderaadt #include <sys/device.h>
3516bde9efSjason
36489e49f9Smiod #include <uvm/uvm_extern.h>
3734fbf6deSderaadt
3850ce9ee0Sniklas #include <machine/autoconf.h> /* badaddr proto */
3950ce9ee0Sniklas
4034fbf6deSderaadt #include <dev/pci/pcireg.h>
4134fbf6deSderaadt #include <dev/pci/pcivar.h>
4234fbf6deSderaadt #include <alpha/pci/lcareg.h>
4334fbf6deSderaadt #include <alpha/pci/lcavar.h>
4434fbf6deSderaadt
45c4071fd1Smillert void lca_attach_hook(struct device *, struct device *,
46c4071fd1Smillert struct pcibus_attach_args *);
47c4071fd1Smillert int lca_bus_maxdevs(void *, int);
48c4071fd1Smillert pcitag_t lca_make_tag(void *, int, int, int);
49c4071fd1Smillert void lca_decompose_tag(void *, pcitag_t, int *, int *,
50c4071fd1Smillert int *);
51b1926db3Smiod int lca_conf_size(void *, pcitag_t);
52c4071fd1Smillert pcireg_t lca_conf_read(void *, pcitag_t, int);
53c4071fd1Smillert void lca_conf_write(void *, pcitag_t, int, pcireg_t);
5434fbf6deSderaadt
55417eba8cSderaadt void
lca_pci_init(pc,v)56417eba8cSderaadt lca_pci_init(pc, v)
57417eba8cSderaadt pci_chipset_tag_t pc;
58417eba8cSderaadt void *v;
5934fbf6deSderaadt {
6034fbf6deSderaadt
61417eba8cSderaadt pc->pc_conf_v = v;
62417eba8cSderaadt pc->pc_attach_hook = lca_attach_hook;
63417eba8cSderaadt pc->pc_bus_maxdevs = lca_bus_maxdevs;
64417eba8cSderaadt pc->pc_make_tag = lca_make_tag;
65417eba8cSderaadt pc->pc_decompose_tag = lca_decompose_tag;
66b1926db3Smiod pc->pc_conf_size = lca_conf_size;
67417eba8cSderaadt pc->pc_conf_read = lca_conf_read;
68417eba8cSderaadt pc->pc_conf_write = lca_conf_write;
6934fbf6deSderaadt }
7034fbf6deSderaadt
71417eba8cSderaadt void
lca_attach_hook(parent,self,pba)72417eba8cSderaadt lca_attach_hook(parent, self, pba)
73417eba8cSderaadt struct device *parent, *self;
74417eba8cSderaadt struct pcibus_attach_args *pba;
75417eba8cSderaadt {
76417eba8cSderaadt }
77417eba8cSderaadt
78417eba8cSderaadt int
lca_bus_maxdevs(cpv,busno)79417eba8cSderaadt lca_bus_maxdevs(cpv, busno)
80417eba8cSderaadt void *cpv;
81417eba8cSderaadt int busno;
82417eba8cSderaadt {
83417eba8cSderaadt
84417eba8cSderaadt if (busno == 0)
85417eba8cSderaadt return 16;
86417eba8cSderaadt else
87417eba8cSderaadt return 32;
88417eba8cSderaadt }
89417eba8cSderaadt
90417eba8cSderaadt pcitag_t
lca_make_tag(cpv,b,d,f)91417eba8cSderaadt lca_make_tag(cpv, b, d, f)
92417eba8cSderaadt void *cpv;
93417eba8cSderaadt int b, d, f;
94417eba8cSderaadt {
95417eba8cSderaadt
96417eba8cSderaadt return (b << 16) | (d << 11) | (f << 8);
97417eba8cSderaadt }
98417eba8cSderaadt
99417eba8cSderaadt void
lca_decompose_tag(cpv,tag,bp,dp,fp)100417eba8cSderaadt lca_decompose_tag(cpv, tag, bp, dp, fp)
101417eba8cSderaadt void *cpv;
102417eba8cSderaadt pcitag_t tag;
103417eba8cSderaadt int *bp, *dp, *fp;
104417eba8cSderaadt {
105417eba8cSderaadt
106417eba8cSderaadt if (bp != NULL)
107417eba8cSderaadt *bp = (tag >> 16) & 0xff;
108417eba8cSderaadt if (dp != NULL)
109417eba8cSderaadt *dp = (tag >> 11) & 0x1f;
110417eba8cSderaadt if (fp != NULL)
111417eba8cSderaadt *fp = (tag >> 8) & 0x7;
112417eba8cSderaadt }
113417eba8cSderaadt
114b1926db3Smiod int
lca_conf_size(void * cpv,pcitag_t tag)115b1926db3Smiod lca_conf_size(void *cpv, pcitag_t tag)
116b1926db3Smiod {
117b1926db3Smiod return PCI_CONFIG_SPACE_SIZE;
118b1926db3Smiod }
119b1926db3Smiod
120417eba8cSderaadt pcireg_t
lca_conf_read(cpv,tag,offset)121417eba8cSderaadt lca_conf_read(cpv, tag, offset)
122417eba8cSderaadt void *cpv;
123417eba8cSderaadt pcitag_t tag;
124417eba8cSderaadt int offset;
125417eba8cSderaadt {
126417eba8cSderaadt struct lca_config *lcp = cpv;
127417eba8cSderaadt pcireg_t *datap, data;
128417eba8cSderaadt int s, secondary, device, ba;
129417eba8cSderaadt
1303a630e3fSniklas s = 0; /* XXX gcc -Wuninitialized */
1313a630e3fSniklas
1329239aac6Smiod alpha_mb();
1339239aac6Smiod REGVAL64(LCA_IOC_STAT0) = REGVAL64(LCA_IOC_STAT0);
1349239aac6Smiod alpha_mb();
1359239aac6Smiod
136417eba8cSderaadt /* secondary if bus # != 0 */
137*adde9fa4Smiod pci_decompose_tag(&lcp->lc_pc, tag, &secondary, &device, NULL);
138417eba8cSderaadt if (secondary) {
139417eba8cSderaadt s = splhigh();
14050ce9ee0Sniklas alpha_mb();
141417eba8cSderaadt REGVAL(LCA_IOC_CONF) = 0x01;
14250ce9ee0Sniklas alpha_mb();
143417eba8cSderaadt } else {
144417eba8cSderaadt /*
145417eba8cSderaadt * on the LCA, must frob the tag used for
146417eba8cSderaadt * devices on the primary bus, in the same ways
147417eba8cSderaadt * as is used by type 1 configuration cycles
148417eba8cSderaadt * on PCs.
149417eba8cSderaadt */
150417eba8cSderaadt tag = (1 << (device + 11)) | (tag & 0x7ff);
151417eba8cSderaadt }
152417eba8cSderaadt
15350ce9ee0Sniklas datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(LCA_PCI_CONF |
154417eba8cSderaadt tag << 5UL | /* XXX */
15534fbf6deSderaadt (offset & ~0x03) << 5 | /* XXX */
15634fbf6deSderaadt 0 << 5 | /* XXX */
15734fbf6deSderaadt 0x3 << 3); /* XXX */
158417eba8cSderaadt data = (pcireg_t)-1;
1599239aac6Smiod if (!(ba = badaddr(datap, sizeof *datap))) {
1609239aac6Smiod if (REGVAL64(LCA_IOC_STAT0) & IOC_STAT0_ERR) {
1619239aac6Smiod alpha_mb();
1629239aac6Smiod REGVAL64(LCA_IOC_STAT0) = REGVAL64(LCA_IOC_STAT0);
1639239aac6Smiod alpha_mb();
1649239aac6Smiod } else
16534fbf6deSderaadt data = *datap;
1669239aac6Smiod }
16734fbf6deSderaadt
16834fbf6deSderaadt if (secondary) {
16950ce9ee0Sniklas alpha_mb();
170417eba8cSderaadt REGVAL(LCA_IOC_CONF) = 0x00;
17150ce9ee0Sniklas alpha_mb();
17234fbf6deSderaadt splx(s);
17334fbf6deSderaadt }
17434fbf6deSderaadt
17534fbf6deSderaadt #if 0
176417eba8cSderaadt printf("lca_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg,
177417eba8cSderaadt data, datap, ba ? " (badaddr)" : "");
17834fbf6deSderaadt #endif
17934fbf6deSderaadt
18034fbf6deSderaadt return data;
18134fbf6deSderaadt }
18234fbf6deSderaadt
18334fbf6deSderaadt void
lca_conf_write(cpv,tag,offset,data)18434fbf6deSderaadt lca_conf_write(cpv, tag, offset, data)
18534fbf6deSderaadt void *cpv;
186417eba8cSderaadt pcitag_t tag;
187417eba8cSderaadt int offset;
188417eba8cSderaadt pcireg_t data;
18934fbf6deSderaadt {
190417eba8cSderaadt struct lca_config *lcp = cpv;
191417eba8cSderaadt pcireg_t *datap;
192417eba8cSderaadt int s, secondary, device;
19334fbf6deSderaadt
1943a630e3fSniklas s = 0; /* XXX gcc -Wuninitialized */
1953a630e3fSniklas
196417eba8cSderaadt /* secondary if bus # != 0 */
197*adde9fa4Smiod pci_decompose_tag(&lcp->lc_pc, tag, &secondary, &device, NULL);
19834fbf6deSderaadt if (secondary) {
19934fbf6deSderaadt s = splhigh();
20050ce9ee0Sniklas alpha_mb();
201417eba8cSderaadt REGVAL(LCA_IOC_CONF) = 0x01;
20250ce9ee0Sniklas alpha_mb();
203417eba8cSderaadt } else {
204417eba8cSderaadt /*
205417eba8cSderaadt * on the LCA, must frob the tag used for
206417eba8cSderaadt * devices on the primary bus, in the same ways
207417eba8cSderaadt * as is used by type 1 configuration cycles
208417eba8cSderaadt * on PCs.
209417eba8cSderaadt */
210417eba8cSderaadt tag = (1 << (device + 11)) | (tag & 0x7ff);
21134fbf6deSderaadt }
21234fbf6deSderaadt
21350ce9ee0Sniklas datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(LCA_PCI_CONF |
214417eba8cSderaadt tag << 5UL | /* XXX */
21534fbf6deSderaadt (offset & ~0x03) << 5 | /* XXX */
21634fbf6deSderaadt 0 << 5 | /* XXX */
21734fbf6deSderaadt 0x3 << 3); /* XXX */
21834fbf6deSderaadt *datap = data;
21934fbf6deSderaadt
22034fbf6deSderaadt if (secondary) {
22150ce9ee0Sniklas alpha_mb();
222417eba8cSderaadt REGVAL(LCA_IOC_CONF) = 0x00;
22350ce9ee0Sniklas alpha_mb();
22434fbf6deSderaadt splx(s);
22534fbf6deSderaadt }
22634fbf6deSderaadt
22734fbf6deSderaadt #if 0
228417eba8cSderaadt printf("lca_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
229417eba8cSderaadt reg, data, datap);
23034fbf6deSderaadt #endif
23134fbf6deSderaadt }
232