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