i80312_pci.c revision 1.1 1 /* $NetBSD: i80312_pci.c,v 1.1 2001/11/09 03:27:51 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2001 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * PCI configuration support for i80312 Companion I/O chip.
40 */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45
46 #include <uvm/uvm_extern.h>
47
48 #include <machine/bus.h>
49
50 #include <arm/xscale/i80312reg.h>
51 #include <arm/xscale/i80312var.h>
52
53 #include <dev/pci/ppbreg.h>
54
55 void i80312_pci_attach_hook(struct device *, struct device *,
56 struct pcibus_attach_args *);
57 int i80312_pci_bus_maxdevs(void *, int);
58 pcitag_t i80312_pci_make_tag(void *, int, int, int);
59 void i80312_pci_decompose_tag(void *, pcitag_t, int *, int *,
60 int *);
61 pcireg_t i80312_pci_conf_read(void *, pcitag_t, int);
62 void i80312_pci_conf_write(void *, pcitag_t, int, pcireg_t);
63
64 #define PCI_CONF_LOCK(s) (s) = disable_interrupts(I32_bit)
65 #define PCI_CONF_UNLOCK(s) restore_interrupts((s))
66
67 void
68 i80312_pci_init(pci_chipset_tag_t pc, void *cookie)
69 {
70
71 pc->pc_conf_v = cookie;
72 pc->pc_attach_hook = i80312_pci_attach_hook;
73 pc->pc_bus_maxdevs = i80312_pci_bus_maxdevs;
74 pc->pc_make_tag = i80312_pci_make_tag;
75 pc->pc_decompose_tag = i80312_pci_decompose_tag;
76 pc->pc_conf_read = i80312_pci_conf_read;
77 pc->pc_conf_write = i80312_pci_conf_write;
78 }
79
80 void
81 i80312_pci_attach_hook(struct device *parent, struct device *self,
82 struct pcibus_attach_args *pba)
83 {
84
85 /* Nothing to do. */
86 }
87
88 int
89 i80312_pci_bus_maxdevs(void *v, int busno)
90 {
91
92 return (32);
93 }
94
95 pcitag_t
96 i80312_pci_make_tag(void *v, int b, int d, int f)
97 {
98
99 return ((b << 16) | (d << 11) | (f << 8));
100 }
101
102 void
103 i80312_pci_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *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
114 struct pciconf_state {
115 bus_addr_t ps_addr_reg;
116 bus_addr_t ps_data_reg;
117 uint32_t ps_addr_val;
118
119 int ps_b, ps_d, ps_f;
120 };
121
122 static int
123 i80312_pci_conf_setup(struct i80312_softc *sc, pcitag_t tag, int offset,
124 struct pciconf_state *ps)
125 {
126 pcireg_t binfo;
127 int pbus, sbus;
128
129 i80312_pci_decompose_tag(sc, tag, &ps->ps_b, &ps->ps_d, &ps->ps_f);
130
131 binfo = bus_space_read_4(sc->sc_st, sc->sc_ppb_sh, PPB_REG_BUSINFO);
132 pbus = PPB_BUSINFO_PRIMARY(binfo);
133 sbus = PPB_BUSINFO_SECONDARY(binfo);
134
135 /*
136 * If the bus # is the Primary bus #, use the Primary
137 * Address/Data registers, otherwise use the Secondary
138 * Address/Data registers.
139 */
140 if (ps->ps_b == pbus) {
141 ps->ps_addr_reg = I80312_ATU_POCCA;
142 ps->ps_data_reg = I80312_ATU_POCCD;
143 } else {
144 ps->ps_addr_reg = I80312_ATU_SOCCA;
145 ps->ps_data_reg = I80312_ATU_SOCCD;
146 }
147
148 /*
149 * If the bus # is the Primary or Secondary bus #, then use
150 * Type 0 cycles, else use Type 1.
151 *
152 * XXX We should filter out all non-private devices here!
153 * XXX How does private space interact with PCI-PCI bridges?
154 */
155 if (ps->ps_b == pbus || ps->ps_b == sbus) {
156 if (ps->ps_d > (31 - 11))
157 return (1);
158 ps->ps_addr_val = (1U << (ps->ps_d + 11)) | (ps->ps_f << 8) |
159 offset;
160 } else {
161 /* The tag is already in the correct format. */
162 ps->ps_addr_val = tag | offset | 1;
163 }
164
165 return (0);
166 }
167
168 pcireg_t
169 i80312_pci_conf_read(void *v, pcitag_t tag, int offset)
170 {
171 struct i80312_softc *sc = v;
172 struct pciconf_state ps;
173 vaddr_t va;
174 pcireg_t rv;
175 u_int s;
176
177 if (i80312_pci_conf_setup(sc, tag, offset, &ps))
178 return ((pcireg_t) -1);
179
180 PCI_CONF_LOCK(s);
181
182 bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_addr_reg,
183 ps.ps_addr_val);
184
185 #if 1
186 va = (vaddr_t) bus_space_vaddr(sc->sc_st, sc->sc_atu_sh);
187 if (badaddr_read((void *) (va + ps.ps_data_reg), sizeof(rv), &rv)) {
188 printf("conf_read: %d/%d/%d bad address\n",
189 ps.ps_b, ps.ps_d, ps.ps_f);
190 rv = (pcireg_t) -1;
191 }
192 #else
193 rv = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ps.ps_data_reg);
194 #endif
195
196 PCI_CONF_UNLOCK(s);
197
198 return (rv);
199 }
200
201 void
202 i80312_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
203 {
204 struct i80312_softc *sc = v;
205 struct pciconf_state ps;
206 u_int s;
207
208 if (i80312_pci_conf_setup(sc, tag, offset, &ps))
209 return;
210
211 PCI_CONF_LOCK(s);
212
213 bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_addr_reg,
214 ps.ps_addr_val);
215 bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_data_reg, val);
216
217 PCI_CONF_UNLOCK(s);
218 }
219