1/*	$OpenBSD: lca_pci.c,v 1.12 2015/10/30 07:51:49 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
45void		lca_attach_hook(struct device *, struct device *,
46		    struct pcibus_attach_args *);
47int		lca_bus_maxdevs(void *, int);
48pcitag_t	lca_make_tag(void *, int, int, int);
49void		lca_decompose_tag(void *, pcitag_t, int *, int *,
50		    int *);
51int		lca_conf_size(void *, pcitag_t);
52pcireg_t	lca_conf_read(void *, pcitag_t, int);
53void		lca_conf_write(void *, pcitag_t, int, pcireg_t);
54
55void
56lca_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
71void
72lca_attach_hook(parent, self, pba)
73	struct device *parent, *self;
74	struct pcibus_attach_args *pba;
75{
76}
77
78int
79lca_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
90pcitag_t
91lca_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
99void
100lca_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
114int
115lca_conf_size(void *cpv, pcitag_t tag)
116{
117	return PCI_CONFIG_SPACE_SIZE;
118}
119
120pcireg_t
121lca_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	alpha_mb();
133	REGVAL64(LCA_IOC_STAT0) = REGVAL64(LCA_IOC_STAT0);
134	alpha_mb();
135
136	/* secondary if bus # != 0 */
137	pci_decompose_tag(&lcp->lc_pc, tag, &secondary, &device, NULL);
138	if (secondary) {
139		s = splhigh();
140		alpha_mb();
141		REGVAL(LCA_IOC_CONF) = 0x01;
142		alpha_mb();
143	} else {
144		/*
145		 * on the LCA, must frob the tag used for
146		 * devices on the primary bus, in the same ways
147		 * as is used by type 1 configuration cycles
148		 * on PCs.
149		 */
150		tag = (1 << (device + 11)) | (tag & 0x7ff);
151	}
152
153	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(LCA_PCI_CONF |
154	    tag << 5UL |					/* XXX */
155	    (offset & ~0x03) << 5 |				/* XXX */
156	    0 << 5 |						/* XXX */
157	    0x3 << 3);						/* XXX */
158	data = (pcireg_t)-1;
159	if (!(ba = badaddr(datap, sizeof *datap))) {
160		if (REGVAL64(LCA_IOC_STAT0) & IOC_STAT0_ERR) {
161			alpha_mb();
162			REGVAL64(LCA_IOC_STAT0) = REGVAL64(LCA_IOC_STAT0);
163			alpha_mb();
164		} else
165			data = *datap;
166	}
167
168	if (secondary) {
169		alpha_mb();
170		REGVAL(LCA_IOC_CONF) = 0x00;
171		alpha_mb();
172		splx(s);
173	}
174
175#if 0
176	printf("lca_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg,
177	    data, datap, ba ? " (badaddr)" : "");
178#endif
179
180	return data;
181}
182
183void
184lca_conf_write(cpv, tag, offset, data)
185	void *cpv;
186	pcitag_t tag;
187	int offset;
188	pcireg_t data;
189{
190	struct lca_config *lcp = cpv;
191	pcireg_t *datap;
192	int s, secondary, device;
193
194	s = 0;					/* XXX gcc -Wuninitialized */
195
196	/* secondary if bus # != 0 */
197	pci_decompose_tag(&lcp->lc_pc, tag, &secondary, &device, NULL);
198	if (secondary) {
199		s = splhigh();
200		alpha_mb();
201		REGVAL(LCA_IOC_CONF) = 0x01;
202		alpha_mb();
203	} else {
204		/*
205		 * on the LCA, must frob the tag used for
206		 * devices on the primary bus, in the same ways
207		 * as is used by type 1 configuration cycles
208		 * on PCs.
209		 */
210		tag = (1 << (device + 11)) | (tag & 0x7ff);
211	}
212
213	datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(LCA_PCI_CONF |
214	    tag << 5UL |					/* XXX */
215	    (offset & ~0x03) << 5 |				/* XXX */
216	    0 << 5 |						/* XXX */
217	    0x3 << 3);						/* XXX */
218	*datap = data;
219
220	if (secondary) {
221		alpha_mb();
222		REGVAL(LCA_IOC_CONF) = 0x00;
223		alpha_mb();
224		splx(s);
225	}
226
227#if 0
228	printf("lca_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag,
229	    reg, data, datap);
230#endif
231}
232