ixp12x0_pci.c revision 1.9 1 /* $NetBSD: ixp12x0_pci.c,v 1.9 2009/03/14 15:36:02 dsl Exp $ */
2 /*
3 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Ichiro FUKUHARA and Naoto Shimazaki.
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 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: ixp12x0_pci.c,v 1.9 2009/03/14 15:36:02 dsl Exp $");
33
34 /*
35 * PCI configuration support for IXP12x0 Network Processor chip.
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/extent.h>
42 #include <sys/malloc.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <arm/ixp12x0/ixp12x0reg.h>
47 #include <arm/ixp12x0/ixp12x0var.h>
48
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pciconf.h>
52
53 #include "opt_pci.h"
54 #include "pci.h"
55
56 void ixp12x0_pci_attach_hook(struct device *, struct device *,
57 struct pcibus_attach_args *);
58 int ixp12x0_pci_bus_maxdevs(void *, int);
59 pcitag_t ixp12x0_pci_make_tag(void *, int, int, int);
60 void ixp12x0_pci_decompose_tag(void *, pcitag_t, int *, int *, int *);
61 pcireg_t ixp12x0_pci_conf_read(void *, pcitag_t, int);
62 void ixp12x0_pci_conf_write(void *, pcitag_t, int, pcireg_t);
63
64 static vaddr_t ixp12x0_pci_conf_setup(void *, struct ixp12x0_softc *, pcitag_t, int);
65
66 #define PCI_CONF_LOCK(s) (s) = disable_interrupts(I32_bit)
67 #define PCI_CONF_UNLOCK(s) restore_interrupts((s))
68
69 #define MAX_PCI_DEVICES 4
70
71 /*
72 * IXM1200 PCI configuration Cycles
73 * Device Address
74 * -------------------------------------
75 * 0 IXP1200 0x0800 - 0x08FF
76 * 1 i21555 0x1000 - 0x10FF
77 * 2 i82559 0x2000 - 0x20FF
78 * 3 PMC expansion 0x4000 - 0x40FF
79 */
80
81 void
82 ixp12x0_pci_init(pci_chipset_tag_t pc, void *cookie)
83 {
84 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
85 struct ixp12x0_softc *sc = cookie;
86 struct extent *ioext, *memext;
87 #endif
88 pc->pc_conf_v = cookie;
89 pc->pc_attach_hook = ixp12x0_pci_attach_hook;
90 pc->pc_bus_maxdevs = ixp12x0_pci_bus_maxdevs;
91 pc->pc_make_tag = ixp12x0_pci_make_tag;
92 pc->pc_decompose_tag = ixp12x0_pci_decompose_tag;
93 pc->pc_conf_read = ixp12x0_pci_conf_read;
94 pc->pc_conf_write = ixp12x0_pci_conf_write;
95
96 #if NPCI > 0 && defined(PCI_NETBSD_CONFIGURE)
97 ioext = extent_create("pciio", 0, IXP12X0_PCI_IO_SIZE - 1,
98 M_DEVBUF, NULL, 0, EX_NOWAIT);
99 /* PCI MEM space is mapped same address as real memory */
100 memext = extent_create("pcimem", IXP12X0_PCI_MEM_HWBASE,
101 IXP12X0_PCI_MEM_HWBASE +
102 IXP12X0_PCI_MEM_SIZE - 1,
103 M_DEVBUF, NULL, 0, EX_NOWAIT);
104 printf("%s: configuring PCI bus\n", sc->sc_dev.dv_xname);
105 pci_configure_bus(pc, ioext, memext, NULL, 0 /* XXX bus = 0 */,
106 arm_dcache_align);
107
108 extent_destroy(ioext);
109 extent_destroy(memext);
110 #endif
111 }
112
113 void
114 pci_conf_interrupt(pc, a, b, c, d, p)
115 pci_chipset_tag_t pc;
116 int a, b, c, d, *p;
117 {
118 /* Nothing */
119 }
120
121 void
122 ixp12x0_pci_attach_hook(struct device *parent, struct device *self, struct pcibus_attach_args *pba)
123 {
124 /* Nothing to do. */
125 }
126
127 int
128 ixp12x0_pci_bus_maxdevs(void *v, int busno)
129 {
130 return(MAX_PCI_DEVICES);
131 }
132
133 pcitag_t
134 ixp12x0_pci_make_tag(v, bus, device, function)
135 void *v;
136 int bus, device, function;
137 {
138 #ifdef PCI_DEBUG
139 printf("ixp12x0_pci_make_tag(v=%p, bus=%d, device=%d, function=%d)\n",
140 v, bus, device, function);
141 #endif
142 return ((bus << 16) | (device << 11) | (function << 8));
143 }
144
145 void
146 ixp12x0_pci_decompose_tag(v, tag, busp, devicep, functionp)
147 void *v;
148 pcitag_t tag;
149 int *busp, *devicep, *functionp;
150 {
151 #ifdef PCI_DEBUG
152 printf("ixp12x0_pci_decompose_tag(v=%p, tag=0x%08lx, bp=%x, dp=%x, fp=%x)\n",
153 v, tag, (int)busp, (int)devicep, (int)functionp);
154 #endif
155
156 if (busp != NULL)
157 *busp = (tag >> 16) & 0xff;
158 if (devicep != NULL)
159 *devicep = (tag >> 11) & 0x1f;
160 if (functionp != NULL)
161 *functionp = (tag >> 8) & 0x7;
162 }
163
164 static vaddr_t
165 ixp12x0_pci_conf_setup(void *v, struct ixp12x0_softc *sc, pcitag_t tag, int offset)
166 {
167 int bus, device, function;
168 vaddr_t addr;
169
170 ixp12x0_pci_decompose_tag(v, tag, &bus, &device, &function);
171
172 if (bus == 0) {
173 /* configuration type 0 */
174 addr = (vaddr_t) bus_space_vaddr(sc->sc_iot, sc->sc_conf0_ioh) +
175 ((1 << (device + 10)) | (offset & ~3));
176 } else {
177 /* configuration type 1 */
178 addr = (vaddr_t) bus_space_vaddr(sc->sc_iot, sc->sc_conf1_ioh) +
179 ((bus << 16) | (device << 11) |
180 (function << 8) | (offset & ~3) | 1);
181 }
182 return addr;
183 }
184
185 pcireg_t
186 ixp12x0_pci_conf_read(void *v, pcitag_t tag, int offset)
187 {
188 struct ixp12x0_softc *sc = v;
189 vaddr_t va = ixp12x0_pci_conf_setup(v, sc, tag, offset);
190 pcireg_t rv;
191 int s;
192
193 #ifdef PCI_DEBUG
194 printf("ixp12x0_pci_conf_read: base=%lx,va=%lx,tag=%lx,offset=%x\n",
195 sc->sc_conf0_ioh, va, tag, offset);
196 #endif
197 if (va == 0)
198 return -1;
199
200 PCI_CONF_LOCK(s);
201
202 if (badaddr_read((void *) va, sizeof(rv), &rv)) {
203 #ifdef PCI_DEBUG
204 printf("conf_read: %lx bad address\n", va);
205 #endif
206 rv = (pcireg_t) - 1;
207 }
208
209 PCI_CONF_UNLOCK(s);
210
211 return rv;
212 }
213
214 void
215 ixp12x0_pci_conf_write(void *v, pcitag_t tag, int offset, pcireg_t val)
216 {
217 struct ixp12x0_softc *sc = v;
218 vaddr_t va = ixp12x0_pci_conf_setup(v, sc, tag, offset);
219 int s;
220
221 #ifdef PCI_DEBUG
222 printf("ixp12x0_pci_conf_write: tag=%lx offset=%x -> va=%lx (base=%lx)\n",
223 tag, offset, va, sc->sc_conf0_ioh);
224 #endif
225
226 PCI_CONF_LOCK(s);
227
228 *(pcireg_t *) va = val;
229
230 PCI_CONF_UNLOCK(s);
231 }
232