rmixl_obio.c revision 1.6 1 /* $NetBSD: rmixl_obio.c,v 1.6 2021/04/24 23:36:43 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2001, 2002, 2003 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 * On-board device autoconfiguration support for RMI {XLP, XLR, XLS} chips
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: rmixl_obio.c,v 1.6 2021/04/24 23:36:43 thorpej Exp $");
44
45 #include "locators.h"
46 #include "pci.h"
47 #define _MIPS_BUS_DMA_PRIVATE
48
49 #include <sys/param.h>
50 #include <sys/bus.h>
51 #include <sys/device.h>
52 #include <sys/extent.h>
53 #include <sys/malloc.h>
54 #include <sys/systm.h>
55
56 #include <mips/int_fmtio.h>
57
58 #include <mips/rmi/rmixlreg.h>
59 #include <mips/rmi/rmixlvar.h>
60 #include <mips/rmi/rmixl_intr.h>
61 #include <mips/rmi/rmixl_obiovar.h>
62 #include <mips/rmi/rmixl_pcievar.h>
63
64 #include <evbmips/rmixl/autoconf.h>
65
66 #ifdef OBIO_DEBUG
67 int obio_rmixl_debug = OBIO_DEBUG;
68 # define DPRINTF(x) do { if (obio_rmixl_debug) printf x ; } while (0)
69 #else
70 # define DPRINTF(x)
71 #endif
72
73 static int obio_match(device_t, cfdata_t, void *);
74 static void obio_attach(device_t, device_t, void *);
75 static int obio_print(void *, const char *);
76 static int obio_search(device_t, cfdata_t, const int *, void *);
77 static void obio_bus_init(struct obio_softc *);
78 static void obio_dma_init_64(bus_dma_tag_t);
79 static int rmixl_addr_error_intr(void *);
80
81
82 CFATTACH_DECL_NEW(obio_rmixl, sizeof(struct obio_softc),
83 obio_match, obio_attach, NULL, NULL);
84
85 int obio_found;
86
87 static int
88 obio_match(device_t parent, cfdata_t cf, void *aux)
89 {
90 struct mainbus_attach_args *aa = aux;
91
92 if (obio_found == 0)
93 if (strncmp(aa->ma_name, cf->cf_name, strlen(cf->cf_name)) == 0)
94 return 1;
95
96 return 0;
97 }
98
99 static void
100 obio_attach(device_t parent, device_t self, void *aux)
101 {
102 struct obio_softc *sc = device_private(self);
103 bus_addr_t ba;
104
105 obio_found = 1;
106 sc->sc_dev = self;
107
108 ba = (bus_addr_t)rmixl_configuration.rc_io_pbase;
109 KASSERT(ba != 0);
110
111 obio_bus_init(sc);
112
113 aprint_normal(" addr %#"PRIxBUSADDR" size %#"PRIxBUSSIZE"\n",
114 ba, (bus_size_t)RMIXL_IO_DEV_SIZE);
115 aprint_naive("\n");
116
117 /*
118 * Attach on-board devices as specified in the kernel config file.
119 */
120 config_search(self, NULL,
121 CFARG_SEARCH, obio_search,
122 CFARG_EOL);
123
124 }
125
126 static int
127 obio_print(void *aux, const char *pnp)
128 {
129 struct obio_attach_args *obio = aux;
130
131 if (obio->obio_addr != OBIOCF_ADDR_DEFAULT) {
132 aprint_normal(" addr %#"PRIxBUSADDR, obio->obio_addr);
133 if (obio->obio_size != OBIOCF_SIZE_DEFAULT)
134 aprint_normal("-%#"PRIxBUSADDR,
135 obio->obio_addr + (obio->obio_size - 1));
136 }
137 if (obio->obio_mult != OBIOCF_MULT_DEFAULT)
138 aprint_normal(" mult %d", obio->obio_mult);
139 if (obio->obio_intr != OBIOCF_INTR_DEFAULT)
140 aprint_normal(" intr %d", obio->obio_intr);
141 if (obio->obio_tmsk != OBIOCF_TMSK_DEFAULT)
142 aprint_normal(" tmsk %d", obio->obio_tmsk);
143
144 return (UNCONF);
145 }
146
147 static int
148 obio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
149 {
150 struct obio_softc *sc = device_private(parent);
151 struct obio_attach_args obio;
152
153 obio.obio_eb_bst = sc->sc_eb_bst;
154 obio.obio_el_bst = sc->sc_el_bst;
155 obio.obio_addr = cf->cf_loc[OBIOCF_ADDR];
156 obio.obio_size = cf->cf_loc[OBIOCF_SIZE];
157 obio.obio_mult = cf->cf_loc[OBIOCF_MULT];
158 obio.obio_intr = cf->cf_loc[OBIOCF_INTR];
159 obio.obio_tmsk = cf->cf_loc[OBIOCF_TMSK];
160 obio.obio_29bit_dmat = sc->sc_29bit_dmat;
161 obio.obio_32bit_dmat = sc->sc_32bit_dmat;
162 obio.obio_64bit_dmat = sc->sc_64bit_dmat;
163
164 if (config_probe(parent, cf, &obio))
165 config_attach(parent, cf, &obio, obio_print, CFARG_EOL);
166
167 return 0;
168 }
169
170 static void
171 obio_bus_init(struct obio_softc *sc)
172 {
173 struct rmixl_config *rcp = &rmixl_configuration;
174 static int done = 0;
175 int error;
176
177 if (done)
178 return;
179 done = 1;
180
181 /* obio (devio) space, Big Endian */
182 if (rcp->rc_obio_eb_memt.bs_cookie == 0)
183 rmixl_obio_eb_bus_mem_init(&rcp->rc_obio_eb_memt, rcp);
184
185 /* obio (devio) space, Little Endian */
186 if (rcp->rc_obio_el_memt.bs_cookie == 0)
187 rmixl_obio_el_bus_mem_init(&rcp->rc_obio_el_memt, rcp);
188
189 /* dma space for all memory, including >= 4GB */
190 if (rcp->rc_dma_tag._cookie == 0)
191 obio_dma_init_64(&rcp->rc_dma_tag);
192 rcp->rc_64bit_dmat = &rcp->rc_dma_tag;
193
194 /* dma space for addr < 4GB */
195 if (rcp->rc_32bit_dmat == NULL) {
196 error = bus_dmatag_subregion(rcp->rc_64bit_dmat,
197 0, (bus_addr_t)1 << 32, &rcp->rc_32bit_dmat, 0);
198 if (error)
199 panic("%s: failed to create 32bit dma tag: %d",
200 __func__, error);
201 }
202
203 /* dma space for addr < 512MB */
204 if (rcp->rc_29bit_dmat == NULL) {
205 error = bus_dmatag_subregion(rcp->rc_32bit_dmat,
206 0, (bus_addr_t)1 << 29, &rcp->rc_29bit_dmat, 0);
207 if (error)
208 panic("%s: failed to create 29bit dma tag: %d",
209 __func__, error);
210 }
211
212 sc->sc_base = (bus_addr_t)rcp->rc_io_pbase;
213 sc->sc_size = (bus_size_t)RMIXL_IO_DEV_SIZE;
214 sc->sc_eb_bst = (bus_space_tag_t)&rcp->rc_obio_eb_memt;
215 sc->sc_el_bst = (bus_space_tag_t)&rcp->rc_obio_el_memt;
216 sc->sc_29bit_dmat = rcp->rc_29bit_dmat;
217 sc->sc_32bit_dmat = rcp->rc_32bit_dmat;
218 sc->sc_64bit_dmat = rcp->rc_64bit_dmat;
219 }
220
221 static void
222 obio_dma_init_64(bus_dma_tag_t t)
223 {
224 t->_cookie = t;
225 t->_wbase = 0;
226 t->_bounce_alloc_lo = 0;
227 t->_bounce_alloc_hi = 0;
228 t->_dmamap_ops = mips_bus_dmamap_ops;
229 t->_dmamem_ops = mips_bus_dmamem_ops;
230 t->_dmatag_ops = mips_bus_dmatag_ops;
231 }
232
233 void
234 rmixl_addr_error_init(void)
235 {
236 uint32_t r;
237
238 /*
239 * activate error addr detection on all (configurable) devices
240 * preserve reserved bit fields
241 * note some of these bits are read-only (writes are ignored)
242 */
243 r = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_DEVICE_MASK);
244 r |= ~(__BITS(19,16) | __BITS(10,9) | __BITS(7,5));
245 RMIXL_IOREG_WRITE(RMIXL_ADDR_ERR_DEVICE_MASK, r);
246
247 /*
248 * enable the address error interrupts
249 * "upgrade" cache and CPU errors to A1
250 */
251 #define _ADDR_ERR_DEVSTAT_A1 (__BIT(8) | __BIT(1) | __BIT(0))
252 #define _ADDR_ERR_RESV \
253 (__BITS(31,21) | __BITS(15,14) | __BITS(10,9) | __BITS(7,2))
254 #define _BITERR_INT_EN_RESV (__BITS(31,8) | __BIT(4))
255
256 r = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_EN);
257 r &= _ADDR_ERR_RESV;
258 r |= ~_ADDR_ERR_RESV;
259 RMIXL_IOREG_WRITE(RMIXL_ADDR_ERR_AERR0_EN, r);
260
261 r = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_UPG);
262 r &= _ADDR_ERR_RESV;
263 r |= _ADDR_ERR_DEVSTAT_A1;
264 RMIXL_IOREG_WRITE(RMIXL_ADDR_ERR_AERR0_UPG, r);
265
266 /*
267 * clear the log regs and the dev stat (interrupt status) regs
268 * "Write any value to bit[0] to clear"
269 */
270 r = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR1_CLEAR);
271 RMIXL_IOREG_WRITE(RMIXL_ADDR_ERR_AERR1_CLEAR, r);
272
273 /*
274 * enable the double bit error interrupts
275 * (assume reserved bits, which are read-only, are ignored)
276 */
277 r = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_BITERR_INT_EN);
278 r &= _BITERR_INT_EN_RESV;
279 r |= __BITS(7,5);
280 RMIXL_IOREG_WRITE(RMIXL_ADDR_ERR_BITERR_INT_EN, r);
281
282 /*
283 * establish address error ISR
284 * XXX assuming "int 16 (bridge_tb)" is our irq
285 * XXX is true for XLS family only
286 */
287 if (cpu_rmixls(mips_options.mips_cpu))
288 rmixl_intr_establish(16, 1, IPL_HIGH,
289 RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
290 rmixl_addr_error_intr, NULL, false);
291 }
292
293 int
294 rmixl_addr_error_check(void)
295 {
296 uint32_t aerr0_devstat;
297 uint32_t aerr0_log1;
298 uint32_t aerr0_log2;
299 uint32_t aerr0_log3;
300 uint32_t aerr1_devstat;
301 uint32_t aerr1_log1;
302 uint32_t aerr1_log2;
303 uint32_t aerr1_log3;
304 uint32_t sbe_counts;
305 uint32_t dbe_counts;
306
307 aerr0_devstat = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_DEVSTAT);
308 aerr0_log1 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_LOG1);
309 aerr0_log2 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_LOG2);
310 aerr0_log3 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_LOG3);
311
312 aerr1_devstat = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR1_DEVSTAT);
313 aerr1_log1 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR1_LOG1);
314 aerr1_log2 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR1_LOG2);
315 aerr1_log3 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR1_LOG3);
316
317 sbe_counts = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_SBE_COUNTS);
318 dbe_counts = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_DBE_COUNTS);
319
320 if (aerr0_log1|aerr0_log2|aerr0_log3
321 |aerr1_log1|aerr1_log2|aerr1_log3
322 |dbe_counts) {
323 printf("aerr0: stat %#x, logs: %#x, %#x, %#x\n",
324 aerr0_devstat, aerr0_log1, aerr0_log2, aerr0_log2);
325 printf("aerr1: stat %#x, logs: %#x, %#x, %#x\n",
326 aerr1_devstat, aerr1_log1, aerr1_log2, aerr1_log2);
327 printf("1-bit errors: %#x, 2-bit errors: %#x\n",
328 sbe_counts, dbe_counts);
329 return 1;
330 }
331 return 0;
332 }
333
334 static int
335 rmixl_addr_error_intr(void *arg)
336 {
337 int err;
338
339 err = rmixl_addr_error_check();
340 if (err != 0) {
341 #if DDB
342 printf("%s\n", __func__);
343 Debugger();
344 #endif
345 panic("Address Error");
346 }
347 return 1;
348 }
349