pq3obio.c revision 1.2.4.3 1 1.2.4.2 rmind /* $NetBSD: pq3obio.c,v 1.2.4.3 2011/05/31 03:04:13 rmind Exp $ */
2 1.2.4.2 rmind /*-
3 1.2.4.2 rmind * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4 1.2.4.2 rmind * All rights reserved.
5 1.2.4.2 rmind *
6 1.2.4.2 rmind * This code is derived from software contributed to The NetBSD Foundation
7 1.2.4.2 rmind * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8 1.2.4.2 rmind * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9 1.2.4.2 rmind *
10 1.2.4.2 rmind * This material is based upon work supported by the Defense Advanced Research
11 1.2.4.2 rmind * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12 1.2.4.2 rmind * Contract No. N66001-09-C-2073.
13 1.2.4.2 rmind * Approved for Public Release, Distribution Unlimited
14 1.2.4.2 rmind *
15 1.2.4.2 rmind * Redistribution and use in source and binary forms, with or without
16 1.2.4.2 rmind * modification, are permitted provided that the following conditions
17 1.2.4.2 rmind * are met:
18 1.2.4.2 rmind * 1. Redistributions of source code must retain the above copyright
19 1.2.4.2 rmind * notice, this list of conditions and the following disclaimer.
20 1.2.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
21 1.2.4.2 rmind * notice, this list of conditions and the following disclaimer in the
22 1.2.4.2 rmind * documentation and/or other materials provided with the distribution.
23 1.2.4.2 rmind *
24 1.2.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 1.2.4.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.2.4.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 1.2.4.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 1.2.4.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 1.2.4.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 1.2.4.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 1.2.4.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 1.2.4.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 1.2.4.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 1.2.4.2 rmind * POSSIBILITY OF SUCH DAMAGE.
35 1.2.4.2 rmind */
36 1.2.4.2 rmind
37 1.2.4.2 rmind #define LBC_PRIVATE
38 1.2.4.2 rmind
39 1.2.4.2 rmind #include "locators.h"
40 1.2.4.2 rmind
41 1.2.4.2 rmind #include <sys/cdefs.h>
42 1.2.4.2 rmind
43 1.2.4.2 rmind __KERNEL_RCSID(0, "$NetBSD: pq3obio.c,v 1.2.4.3 2011/05/31 03:04:13 rmind Exp $");
44 1.2.4.2 rmind
45 1.2.4.2 rmind #include <sys/param.h>
46 1.2.4.2 rmind #include <sys/device.h>
47 1.2.4.2 rmind #include <sys/kmem.h>
48 1.2.4.2 rmind #include <sys/bus.h>
49 1.2.4.2 rmind #include <sys/extent.h>
50 1.2.4.2 rmind
51 1.2.4.2 rmind #include <powerpc/booke/cpuvar.h>
52 1.2.4.2 rmind #include <powerpc/booke/e500var.h>
53 1.2.4.2 rmind #include <powerpc/booke/e500reg.h>
54 1.2.4.3 rmind #include <powerpc/booke/obiovar.h>
55 1.2.4.2 rmind
56 1.2.4.2 rmind static int pq3obio_match(device_t, cfdata_t, void *);
57 1.2.4.2 rmind static void pq3obio_attach(device_t, device_t, void *);
58 1.2.4.2 rmind
59 1.2.4.2 rmind CFATTACH_DECL_NEW(pq3obio, sizeof(struct pq3obio_softc),
60 1.2.4.2 rmind pq3obio_match, pq3obio_attach, NULL, NULL);
61 1.2.4.2 rmind
62 1.2.4.2 rmind static int
63 1.2.4.2 rmind pq3obio_match(device_t parent, cfdata_t cf, void *aux)
64 1.2.4.2 rmind {
65 1.2.4.2 rmind
66 1.2.4.2 rmind if (!e500_cpunode_submatch(parent, cf, "lbc", aux))
67 1.2.4.2 rmind return 0;
68 1.2.4.2 rmind
69 1.2.4.2 rmind return 1;
70 1.2.4.2 rmind }
71 1.2.4.2 rmind
72 1.2.4.2 rmind static int
73 1.2.4.2 rmind pq3obio_print(void *aux, const char *pnp)
74 1.2.4.2 rmind {
75 1.2.4.2 rmind struct generic_attach_args * const ga = aux;
76 1.2.4.2 rmind
77 1.2.4.2 rmind if (pnp)
78 1.2.4.2 rmind aprint_normal(" at %s", pnp);
79 1.2.4.2 rmind
80 1.2.4.2 rmind if (ga->ga_cs != OBIOCF_CS_DEFAULT)
81 1.2.4.2 rmind aprint_normal(" cs %d", ga->ga_cs);
82 1.2.4.2 rmind
83 1.2.4.2 rmind if (ga->ga_addr != OBIOCF_ADDR_DEFAULT) {
84 1.2.4.2 rmind aprint_normal(" addr %#x", ga->ga_addr);
85 1.2.4.2 rmind if (ga->ga_size != OBIOCF_SIZE_DEFAULT)
86 1.2.4.2 rmind aprint_normal(" size %#x", ga->ga_size);
87 1.2.4.2 rmind }
88 1.2.4.2 rmind if (ga->ga_irq != OBIOCF_IRQ_DEFAULT)
89 1.2.4.2 rmind aprint_normal(" irq %d", ga->ga_irq);
90 1.2.4.2 rmind
91 1.2.4.2 rmind return UNCONF;
92 1.2.4.2 rmind }
93 1.2.4.2 rmind
94 1.2.4.2 rmind static int
95 1.2.4.2 rmind pq3obio_search(device_t parent, cfdata_t cf, const int *slocs, void *aux)
96 1.2.4.2 rmind {
97 1.2.4.2 rmind struct pq3obio_softc * const sc = device_private(parent);
98 1.2.4.2 rmind struct generic_attach_args ga;
99 1.2.4.2 rmind bool tryagain;
100 1.2.4.2 rmind
101 1.2.4.2 rmind do {
102 1.2.4.2 rmind const struct pq3lbc_softc *lbc;
103 1.2.4.2 rmind ga.ga_name = "obio";
104 1.2.4.2 rmind ga.ga_addr = cf->cf_loc[OBIOCF_ADDR];
105 1.2.4.2 rmind ga.ga_size = cf->cf_loc[OBIOCF_SIZE];
106 1.2.4.2 rmind ga.ga_irq = cf->cf_loc[OBIOCF_IRQ];
107 1.2.4.2 rmind ga.ga_cs = cf->cf_loc[OBIOCF_CS];
108 1.2.4.2 rmind ga.ga_bst = &sc->sc_obio_bst;
109 1.2.4.2 rmind
110 1.2.4.2 rmind if (ga.ga_cs != OBIOCF_CS_DEFAULT) {
111 1.2.4.2 rmind lbc = &sc->sc_lbcs[ga.ga_cs];
112 1.2.4.2 rmind if ((u_int) ga.ga_cs >= __arraycount(sc->sc_lbcs))
113 1.2.4.2 rmind return 0;
114 1.2.4.2 rmind if (ga.ga_addr != OBIOCF_ADDR_DEFAULT) {
115 1.2.4.2 rmind if (ga.ga_addr < lbc->lbc_base
116 1.2.4.2 rmind || ga.ga_addr > lbc->lbc_limit)
117 1.2.4.2 rmind return 0;
118 1.2.4.2 rmind } else {
119 1.2.4.2 rmind ga.ga_addr = lbc->lbc_base;
120 1.2.4.2 rmind }
121 1.2.4.2 rmind } else {
122 1.2.4.2 rmind u_int cs;
123 1.2.4.2 rmind if (ga.ga_addr == OBIOCF_ADDR_DEFAULT)
124 1.2.4.2 rmind return 0;
125 1.2.4.2 rmind for (cs = 0, lbc = sc->sc_lbcs;
126 1.2.4.2 rmind cs < __arraycount(sc->sc_lbcs);
127 1.2.4.2 rmind cs++, lbc++) {
128 1.2.4.2 rmind if (ga.ga_addr >= lbc->lbc_base
129 1.2.4.2 rmind && ga.ga_addr <= lbc->lbc_limit) {
130 1.2.4.2 rmind ga.ga_cs = cs;
131 1.2.4.2 rmind break;
132 1.2.4.2 rmind }
133 1.2.4.2 rmind }
134 1.2.4.2 rmind if (ga.ga_cs == OBIOCF_CS_DEFAULT)
135 1.2.4.2 rmind return 0;
136 1.2.4.2 rmind }
137 1.2.4.2 rmind
138 1.2.4.2 rmind if (ga.ga_size != OBIOCF_SIZE_DEFAULT) {
139 1.2.4.2 rmind if (ga.ga_size >= lbc->lbc_limit - ga.ga_addr)
140 1.2.4.2 rmind return 0;
141 1.2.4.2 rmind } else {
142 1.2.4.2 rmind ga.ga_size = lbc->lbc_limit + 1 - lbc->lbc_base;
143 1.2.4.2 rmind }
144 1.2.4.2 rmind
145 1.2.4.2 rmind tryagain = false;
146 1.2.4.2 rmind if (config_match(parent, cf, &ga) > 0) {
147 1.2.4.2 rmind int floc[OBIOCF_NLOCS] = {
148 1.2.4.2 rmind [OBIOCF_ADDR] = ga.ga_addr,
149 1.2.4.2 rmind [OBIOCF_SIZE] = ga.ga_size,
150 1.2.4.2 rmind [OBIOCF_IRQ] = ga.ga_irq,
151 1.2.4.2 rmind [OBIOCF_CS] = ga.ga_cs,
152 1.2.4.2 rmind };
153 1.2.4.2 rmind config_attach_loc(parent, cf, floc, &ga, pq3obio_print);
154 1.2.4.2 rmind tryagain = (cf->cf_fstate == FSTATE_STAR);
155 1.2.4.2 rmind }
156 1.2.4.2 rmind } while (tryagain);
157 1.2.4.2 rmind
158 1.2.4.2 rmind return (0);
159 1.2.4.2 rmind }
160 1.2.4.2 rmind
161 1.2.4.2 rmind static const char br_msel_strings[8][6] = {
162 1.2.4.2 rmind [__SHIFTOUT(BR_MSEL_GPCM,BR_MSEL)] = "GPCM",
163 1.2.4.2 rmind [__SHIFTOUT(BR_MSEL_FCM,BR_MSEL)] = "FCM",
164 1.2.4.2 rmind [__SHIFTOUT(BR_MSEL_SDRAM,BR_MSEL)] = "SDRAM",
165 1.2.4.2 rmind [__SHIFTOUT(BR_MSEL_UPMA,BR_MSEL)] = "UPMA",
166 1.2.4.2 rmind [__SHIFTOUT(BR_MSEL_UPMB,BR_MSEL)] = "UPMB",
167 1.2.4.2 rmind [__SHIFTOUT(BR_MSEL_UPMC,BR_MSEL)] = "UPMC",
168 1.2.4.2 rmind };
169 1.2.4.2 rmind
170 1.2.4.2 rmind static void
171 1.2.4.2 rmind pq3obio_attach(device_t parent, device_t self, void *aux)
172 1.2.4.2 rmind {
173 1.2.4.2 rmind struct cpunode_softc * const psc = device_private(parent);
174 1.2.4.2 rmind struct pq3obio_softc * const sc = device_private(self);
175 1.2.4.2 rmind struct cpunode_attach_args * const cna = aux;
176 1.2.4.2 rmind struct cpunode_locators * const cnl = &cna->cna_locs;
177 1.2.4.2 rmind struct powerpc_bus_space * const t = &sc->sc_obio_bst;
178 1.2.4.2 rmind u_int found = 0;
179 1.2.4.2 rmind int error;
180 1.2.4.2 rmind
181 1.2.4.2 rmind psc->sc_children |= cna->cna_childmask;
182 1.2.4.2 rmind sc->sc_dev = self;
183 1.2.4.2 rmind sc->sc_bst = cna->cna_memt;
184 1.2.4.2 rmind
185 1.2.4.2 rmind error = bus_space_map(sc->sc_bst, cnl->cnl_addr, cnl->cnl_size, 0,
186 1.2.4.2 rmind &sc->sc_bsh);
187 1.2.4.2 rmind if (error) {
188 1.2.4.2 rmind aprint_error("failed to map registers: %d\n", error);
189 1.2.4.2 rmind return;
190 1.2.4.2 rmind }
191 1.2.4.2 rmind
192 1.2.4.2 rmind for (u_int i = 0; i < 8; i++) {
193 1.2.4.2 rmind struct pq3lbc_softc * const lbc = &sc->sc_lbcs[i];
194 1.2.4.2 rmind uint32_t br = bus_space_read_4(sc->sc_bst, sc->sc_bsh, BRn(i));
195 1.2.4.2 rmind if (br & BR_V) {
196 1.2.4.2 rmind found++;
197 1.2.4.2 rmind lbc->lbc_br = br;
198 1.2.4.2 rmind lbc->lbc_or = bus_space_read_4(sc->sc_bst,
199 1.2.4.2 rmind sc->sc_bsh, ORn(i));
200 1.2.4.2 rmind lbc->lbc_base = lbc->lbc_br & BR_BA & lbc->lbc_or;
201 1.2.4.2 rmind lbc->lbc_limit = lbc->lbc_base + ~(lbc->lbc_or & OR_AM);
202 1.2.4.2 rmind }
203 1.2.4.2 rmind }
204 1.2.4.2 rmind
205 1.2.4.2 rmind aprint_normal(": %u of 8 ports enabled\n", found);
206 1.2.4.2 rmind if (found == 0)
207 1.2.4.2 rmind return;
208 1.2.4.2 rmind
209 1.2.4.2 rmind t->pbs_base = 0xffffffff;
210 1.2.4.2 rmind t->pbs_limit = 0;
211 1.2.4.2 rmind t->pbs_flags = _BUS_SPACE_BIG_ENDIAN;
212 1.2.4.2 rmind
213 1.2.4.2 rmind u_int sorted[found];
214 1.2.4.2 rmind u_int nsorted = 0;
215 1.2.4.2 rmind
216 1.2.4.2 rmind for (u_int i = 0; i < 8; i++) {
217 1.2.4.2 rmind struct pq3lbc_softc * const lbc = &sc->sc_lbcs[i];
218 1.2.4.2 rmind if ((lbc->lbc_br & BR_V) == 0)
219 1.2.4.2 rmind continue;
220 1.2.4.2 rmind
221 1.2.4.2 rmind u_int j;
222 1.2.4.2 rmind for (j = 0; j < nsorted; j++) {
223 1.2.4.2 rmind if (lbc->lbc_base < sc->sc_lbcs[sorted[j]].lbc_base)
224 1.2.4.2 rmind break;
225 1.2.4.2 rmind }
226 1.2.4.2 rmind for (u_int k = ++nsorted; k > j; k--) {
227 1.2.4.2 rmind sorted[k] = sorted[k - 1];
228 1.2.4.2 rmind }
229 1.2.4.2 rmind sorted[j] = i;
230 1.2.4.2 rmind
231 1.2.4.2 rmind if (lbc->lbc_base < t->pbs_base)
232 1.2.4.2 rmind t->pbs_base = lbc->lbc_base;
233 1.2.4.2 rmind if (lbc->lbc_limit > t->pbs_limit)
234 1.2.4.2 rmind t->pbs_limit = lbc->lbc_limit;
235 1.2.4.2 rmind #if 0
236 1.2.4.2 rmind aprint_normal_dev(sc->sc_dev,
237 1.2.4.2 rmind "cs%u: br=%#x or=%#x", i, lbc->lbc_br, lbc->lbc_or);
238 1.2.4.2 rmind #endif
239 1.2.4.2 rmind u_int n = ffs(~(lbc->lbc_or & OR_AM) + 1) - 1;
240 1.2.4.2 rmind aprint_normal_dev(sc->sc_dev,
241 1.2.4.2 rmind "cs%u: %u%cB %u-bit %s region at %#x\n",
242 1.2.4.2 rmind i,
243 1.2.4.2 rmind 1 << (n % 10), " KMGTPE"[n / 10],
244 1.2.4.2 rmind 1 << (__SHIFTOUT(lbc->lbc_br, BR_PS) + 2),
245 1.2.4.2 rmind br_msel_strings[__SHIFTOUT(lbc->lbc_br,BR_MSEL)],
246 1.2.4.2 rmind lbc->lbc_base);
247 1.2.4.2 rmind }
248 1.2.4.2 rmind
249 1.2.4.2 rmind error = bus_space_init(t, device_xname(sc->sc_dev), NULL, 0);
250 1.2.4.2 rmind if (error) {
251 1.2.4.2 rmind aprint_error_dev(sc->sc_dev,
252 1.2.4.2 rmind "failed to initialize obio bus space: %d\n", error);
253 1.2.4.2 rmind return;
254 1.2.4.2 rmind }
255 1.2.4.2 rmind
256 1.2.4.2 rmind /*
257 1.2.4.2 rmind * We've created a bus space which covers all the chip selects
258 1.2.4.2 rmind * but there might be gaps inbetween them. We need to make sure
259 1.2.4.2 rmind * bus_space_map will fail for those. To do that, we reserve
260 1.2.4.2 rmind * the gaps between the chip-selects.
261 1.2.4.2 rmind */
262 1.2.4.2 rmind for (u_int i = 1; i < found; i++) {
263 1.2.4.2 rmind struct pq3lbc_softc * const lbc_lo = &sc->sc_lbcs[sorted[i-1]];
264 1.2.4.2 rmind struct pq3lbc_softc * const lbc_hi = &sc->sc_lbcs[sorted[i]];
265 1.2.4.2 rmind if (lbc_lo->lbc_limit + 1 == lbc_hi->lbc_base)
266 1.2.4.2 rmind continue;
267 1.2.4.2 rmind error = extent_alloc_region(t->pbs_extent,
268 1.2.4.2 rmind lbc_lo->lbc_limit + 1,
269 1.2.4.2 rmind lbc_hi->lbc_base - (lbc_lo->lbc_limit + 1), 0);
270 1.2.4.2 rmind if (error) {
271 1.2.4.2 rmind aprint_error_dev(sc->sc_dev,
272 1.2.4.2 rmind "failed to reserve %#x..%#x: %d\n",
273 1.2.4.2 rmind lbc_lo->lbc_limit + 1,
274 1.2.4.2 rmind lbc_hi->lbc_base - 1,
275 1.2.4.2 rmind error);
276 1.2.4.2 rmind return;
277 1.2.4.2 rmind }
278 1.2.4.2 rmind }
279 1.2.4.2 rmind
280 1.2.4.2 rmind /*
281 1.2.4.2 rmind * Now we can do the device configuration to find what might
282 1.2.4.2 rmind * be using obio.
283 1.2.4.2 rmind */
284 1.2.4.2 rmind int locs[OBIOCF_NLOCS];
285 1.2.4.2 rmind locs[OBIOCF_ADDR] = OBIOCF_ADDR_DEFAULT;
286 1.2.4.2 rmind locs[OBIOCF_SIZE] = OBIOCF_SIZE_DEFAULT;
287 1.2.4.2 rmind locs[OBIOCF_CS] = OBIOCF_CS_DEFAULT;
288 1.2.4.2 rmind config_search_loc(pq3obio_search, self, "obio", locs, NULL);
289 1.2.4.2 rmind }
290