i80312_pci.c revision 1.2 1 /* $NetBSD: i80312_pci.c,v 1.2 2001/11/09 18:04:10 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 bus_addr_t ps_csr_reg;
118 uint32_t ps_addr_val;
119
120 int ps_b, ps_d, ps_f;
121 };
122
123 static int
124 i80312_pci_conf_setup(struct i80312_softc *sc, pcitag_t tag, int offset,
125 struct pciconf_state *ps)
126 {
127 pcireg_t binfo;
128 int pbus, sbus;
129
130 i80312_pci_decompose_tag(sc, tag, &ps->ps_b, &ps->ps_d, &ps->ps_f);
131
132 binfo = bus_space_read_4(sc->sc_st, sc->sc_ppb_sh, PPB_REG_BUSINFO);
133 pbus = PPB_BUSINFO_PRIMARY(binfo);
134 sbus = PPB_BUSINFO_SECONDARY(binfo);
135
136 /*
137 * If the bus # is the Primary bus #, use the Primary
138 * Address/Data registers, otherwise use the Secondary
139 * Address/Data registers.
140 */
141 if (ps->ps_b == pbus) {
142 ps->ps_addr_reg = I80312_ATU_POCCA;
143 ps->ps_data_reg = I80312_ATU_POCCD;
144 ps->ps_csr_reg = PCI_COMMAND_STATUS_REG;
145 } else {
146 ps->ps_addr_reg = I80312_ATU_SOCCA;
147 ps->ps_data_reg = I80312_ATU_SOCCD;
148 ps->ps_csr_reg = I80312_ATU_SACS;
149 }
150
151 /*
152 * If the bus # is the Primary or Secondary bus #, then use
153 * Type 0 cycles, else use Type 1.
154 *
155 * XXX We should filter out all non-private devices here!
156 * XXX How does private space interact with PCI-PCI bridges?
157 */
158 if (ps->ps_b == pbus || ps->ps_b == sbus) {
159 if (ps->ps_d > (31 - 11))
160 return (1);
161 ps->ps_addr_val = (1U << (ps->ps_d + 11)) | (ps->ps_f << 8) |
162 offset;
163 } else {
164 /* The tag is already in the correct format. */
165 ps->ps_addr_val = tag | offset | 1;
166 }
167
168 return (0);
169 }
170
171 pcireg_t
172 i80312_pci_conf_read(void *v, pcitag_t tag, int offset)
173 {
174 struct i80312_softc *sc = v;
175 struct pciconf_state ps;
176 vaddr_t va;
177 pcireg_t rv;
178 u_int s;
179
180 if (i80312_pci_conf_setup(sc, tag, offset, &ps))
181 return ((pcireg_t) -1);
182
183 PCI_CONF_LOCK(s);
184
185 bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_addr_reg,
186 ps.ps_addr_val);
187
188 va = (vaddr_t) bus_space_vaddr(sc->sc_st, sc->sc_atu_sh);
189 if (badaddr_read((void *) (va + ps.ps_data_reg), sizeof(rv), &rv)) {
190 /*
191 * Clear the Master Abort by reading the PCI
192 * Status Register.
193 */
194 (void) bus_space_read_4(sc->sc_st, sc->sc_atu_sh,
195 ps.ps_csr_reg);
196 #if 0
197 printf("conf_read: %d/%d/%d bad address\n",
198 ps.ps_b, ps.ps_d, ps.ps_f);
199 #endif
200 rv = (pcireg_t) -1;
201 }
202
203 PCI_CONF_UNLOCK(s);
204
205 return (rv);
206 }
207
208 void
209 i80312_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
210 {
211 struct i80312_softc *sc = v;
212 struct pciconf_state ps;
213 u_int s;
214
215 if (i80312_pci_conf_setup(sc, tag, offset, &ps))
216 return;
217
218 PCI_CONF_LOCK(s);
219
220 bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_addr_reg,
221 ps.ps_addr_val);
222 bus_space_write_4(sc->sc_st, sc->sc_atu_sh, ps.ps_data_reg, val);
223
224 PCI_CONF_UNLOCK(s);
225 }
226