prep_pciconf_direct.c revision 1.1 1 /* $NetBSD: prep_pciconf_direct.c,v 1.1 2002/02/24 13:19:08 kleink Exp $ */
2
3 /*
4 * Copyright (c) 2002 Klaus J. Klein. All rights reserved.
5 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
6 * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles M. Hannum.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Machine-specific functions for PCI autoconfiguration.
36 *
37 * This version is specific to the direct-mapped configuration space access
38 * such as implemented on the IBM 27-82650 PCI Bridge/Memory Controller.
39 */
40
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/time.h>
44 #include <sys/systm.h>
45 #include <sys/errno.h>
46 #include <sys/device.h>
47
48 #include <uvm/uvm_extern.h>
49
50 #define _POWERPC_BUS_DMA_PRIVATE
51 #include <machine/bus.h>
52 #include <machine/intr.h>
53 #include <machine/platform.h>
54
55 #include <dev/isa/isavar.h>
56 #include <dev/pci/pcivar.h>
57 #include <dev/pci/pcireg.h>
58 #include <dev/pci/pcidevs.h>
59
60 #ifdef DEBUG
61 #define DPRINTF(x) printf x
62 #else
63 #define DPRINTF(x)
64 #endif
65
66 /* Physical base address and size of PCI configuration space. */
67 #define PCI_DCONF_BASE 0x80800000
68 #define PCI_DCONF_SIZE 0x00800000
69 /* Device function selection. */
70 #define PCI_DCONF_FUNC 0x00000700
71 #define PCI_DCONF_FUNC_SHIFT 8
72 /* Configuration space register selection. */
73 #define PCI_DCONF_REG 0x000000ff
74 #define PCI_DCONF_REG_SHIFT 0
75 /* Device selection; those bits still available. */
76 #define PCI_DCONF_DEV \
77 (~(~(PCI_DCONF_SIZE - 1) | PCI_DCONF_FUNC | PCI_DCONF_REG))
78
79 void prep_pci_direct_attach_hook(struct device *, struct device *,
80 struct pcibus_attach_args *);
81 pcitag_t prep_pci_direct_make_tag(void *, int, int, int);
82 pcireg_t prep_pci_direct_conf_read(void *, pcitag_t, int);
83 void prep_pci_direct_conf_write(void *, pcitag_t, int, pcireg_t);
84 void prep_pci_direct_decompose_tag(void *, pcitag_t, int *, int *, int *);
85
86 void
87 prep_pci_get_chipset_tag_direct(pci_chipset_tag_t pc)
88 {
89
90 pc->pc_conf_v = NULL;
91
92 pc->pc_attach_hook = prep_pci_direct_attach_hook;
93 pc->pc_bus_maxdevs = prep_pci_bus_maxdevs;
94 pc->pc_make_tag = prep_pci_direct_make_tag;
95 pc->pc_conf_read = prep_pci_direct_conf_read;
96 pc->pc_conf_write = prep_pci_direct_conf_write;
97
98 pc->pc_intr_v = NULL;
99
100 pc->pc_intr_map = prep_pci_intr_map;
101 pc->pc_intr_string = prep_pci_intr_string;
102 pc->pc_intr_evcnt = prep_pci_intr_evcnt;
103 pc->pc_intr_establish = prep_pci_intr_establish;
104 pc->pc_intr_disestablish = prep_pci_intr_disestablish;
105
106 pc->pc_conf_interrupt = prep_pci_conf_interrupt;
107 pc->pc_decompose_tag = prep_pci_direct_decompose_tag;
108 pc->pc_conf_hook = prep_pci_conf_hook;
109 }
110
111 void
112 prep_pci_direct_attach_hook(struct device *parent, struct device *self,
113 struct pcibus_attach_args *pba)
114 {
115
116 if (pba->pba_bus == 0)
117 printf(": direct-mapped configuration space access");
118 }
119
120 pcitag_t
121 prep_pci_direct_make_tag(void *v, int bus, int device, int function)
122 {
123 pcitag_t tag;
124
125 if (bus >= 256 || device >= 32 || function >= 8)
126 panic("prep_pci_direct_make_tag: bad request");
127
128 tag = (bus << 16) | (device << 11) | (function << 8);
129
130 return tag;
131 }
132
133 void
134 prep_pci_direct_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp,
135 int *fp)
136 {
137
138 if (bp != NULL)
139 *bp = (tag >> 16) & 0xff;
140 if (dp != NULL)
141 *dp = (tag >> 11) & 0x1f;
142 if (fp != NULL)
143 *fp = (tag >> 8) & 0x7;
144 return;
145 }
146
147 pcireg_t
148 prep_pci_direct_conf_read(void *v, pcitag_t tag, int reg)
149 {
150 pcireg_t data;
151 int bus, device, function;
152 int s;
153
154 prep_pci_direct_decompose_tag(v, tag, &bus, &device, &function);
155
156 if (bus == 0) {
157 /* Check if device selector is within selector range. */
158 if (1 << device & ~PCI_DCONF_DEV) {
159 data = ~0;
160 goto out;
161 }
162 tag = (1 << device) | (function << PCI_DCONF_FUNC_SHIFT);
163 }
164
165 s = splhigh();
166 data = in32rb(PCI_DCONF_BASE | tag | reg);
167 splx(s);
168
169 out:
170 return data;
171 }
172
173 void
174 prep_pci_direct_conf_write(void *v, pcitag_t tag, int reg, pcireg_t data)
175 {
176 int bus, device, function;
177 int s;
178
179 DPRINTF(("prep_pci_direct_conf_write(0x%lx, 0x%x, 0x%02x, 0x%08lx) ",
180 (unsigned long)v, tag, reg, (unsigned long)data));
181
182 prep_pci_direct_decompose_tag(v, tag, &bus, &device, &function);
183
184 if (bus == 0) {
185 /* Check if device selector is within selector range. */
186 if (1 << device & ~PCI_DCONF_DEV) {
187 /* Should never happen. */
188 panic("prep_pci_direct_conf_write: bad device %d",
189 device);
190 }
191 tag = (1 << device) | (function << PCI_DCONF_FUNC_SHIFT);
192 }
193
194 s = splhigh();
195 out32rb(PCI_DCONF_BASE | tag | reg, data);
196 splx(s);
197 }
198