1*aec6f0cfSskrll /* $NetBSD: rmixl_obio.c,v 1.10 2022/09/29 07:00:47 skrll Exp $ */
2290a34a0Smatt
3290a34a0Smatt /*
4290a34a0Smatt * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
5290a34a0Smatt * All rights reserved.
6290a34a0Smatt *
7290a34a0Smatt * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8290a34a0Smatt *
9290a34a0Smatt * Redistribution and use in source and binary forms, with or without
10290a34a0Smatt * modification, are permitted provided that the following conditions
11290a34a0Smatt * are met:
12290a34a0Smatt * 1. Redistributions of source code must retain the above copyright
13290a34a0Smatt * notice, this list of conditions and the following disclaimer.
14290a34a0Smatt * 2. Redistributions in binary form must reproduce the above copyright
15290a34a0Smatt * notice, this list of conditions and the following disclaimer in the
16290a34a0Smatt * documentation and/or other materials provided with the distribution.
17290a34a0Smatt * 3. All advertising materials mentioning features or use of this software
18290a34a0Smatt * must display the following acknowledgement:
19290a34a0Smatt * This product includes software developed for the NetBSD Project by
20290a34a0Smatt * Wasabi Systems, Inc.
21290a34a0Smatt * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22290a34a0Smatt * or promote products derived from this software without specific prior
23290a34a0Smatt * written permission.
24290a34a0Smatt *
25290a34a0Smatt * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26290a34a0Smatt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27290a34a0Smatt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28290a34a0Smatt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29290a34a0Smatt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30290a34a0Smatt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31290a34a0Smatt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32290a34a0Smatt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33290a34a0Smatt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34290a34a0Smatt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35290a34a0Smatt * POSSIBILITY OF SUCH DAMAGE.
36290a34a0Smatt */
37290a34a0Smatt
38290a34a0Smatt /*
39290a34a0Smatt * On-board device autoconfiguration support for RMI {XLP, XLR, XLS} chips
40290a34a0Smatt */
41290a34a0Smatt
42290a34a0Smatt #include <sys/cdefs.h>
43*aec6f0cfSskrll __KERNEL_RCSID(0, "$NetBSD: rmixl_obio.c,v 1.10 2022/09/29 07:00:47 skrll Exp $");
44290a34a0Smatt
45290a34a0Smatt #include "locators.h"
46290a34a0Smatt #include "pci.h"
47b6185cbdSmatt #define _MIPS_BUS_DMA_PRIVATE
48290a34a0Smatt
49290a34a0Smatt #include <sys/param.h>
50b6185cbdSmatt #include <sys/bus.h>
51290a34a0Smatt #include <sys/device.h>
52290a34a0Smatt #include <sys/extent.h>
53b6185cbdSmatt #include <sys/systm.h>
54290a34a0Smatt
55b6185cbdSmatt #include <mips/int_fmtio.h>
56290a34a0Smatt
57290a34a0Smatt #include <mips/rmi/rmixlreg.h>
58290a34a0Smatt #include <mips/rmi/rmixlvar.h>
593e67b512Smatt #include <mips/rmi/rmixl_intr.h>
60290a34a0Smatt #include <mips/rmi/rmixl_obiovar.h>
61290a34a0Smatt #include <mips/rmi/rmixl_pcievar.h>
62290a34a0Smatt
633e67b512Smatt #include <evbmips/rmixl/autoconf.h>
643e67b512Smatt
65290a34a0Smatt #ifdef OBIO_DEBUG
663e67b512Smatt int obio_rmixl_debug = OBIO_DEBUG;
673e67b512Smatt # define DPRINTF(x) do { if (obio_rmixl_debug) printf x ; } while (0)
68290a34a0Smatt #else
69290a34a0Smatt # define DPRINTF(x)
70290a34a0Smatt #endif
71290a34a0Smatt
72290a34a0Smatt static int obio_match(device_t, cfdata_t, void *);
73290a34a0Smatt static void obio_attach(device_t, device_t, void *);
74290a34a0Smatt static int obio_print(void *, const char *);
75290a34a0Smatt static int obio_search(device_t, cfdata_t, const int *, void *);
76290a34a0Smatt static void obio_bus_init(struct obio_softc *);
773e67b512Smatt static void obio_dma_init_64(bus_dma_tag_t);
78290a34a0Smatt static int rmixl_addr_error_intr(void *);
79290a34a0Smatt
80290a34a0Smatt
813e67b512Smatt CFATTACH_DECL_NEW(obio_rmixl, sizeof(struct obio_softc),
82290a34a0Smatt obio_match, obio_attach, NULL, NULL);
83290a34a0Smatt
84290a34a0Smatt int obio_found;
85290a34a0Smatt
86290a34a0Smatt static int
obio_match(device_t parent,cfdata_t cf,void * aux)87290a34a0Smatt obio_match(device_t parent, cfdata_t cf, void *aux)
88290a34a0Smatt {
893e67b512Smatt struct mainbus_attach_args *aa = aux;
903e67b512Smatt
913e67b512Smatt if (obio_found == 0)
923e67b512Smatt if (strncmp(aa->ma_name, cf->cf_name, strlen(cf->cf_name)) == 0)
93290a34a0Smatt return 1;
943e67b512Smatt
953e67b512Smatt return 0;
96290a34a0Smatt }
97290a34a0Smatt
98290a34a0Smatt static void
obio_attach(device_t parent,device_t self,void * aux)99290a34a0Smatt obio_attach(device_t parent, device_t self, void *aux)
100290a34a0Smatt {
101290a34a0Smatt struct obio_softc *sc = device_private(self);
102290a34a0Smatt bus_addr_t ba;
103290a34a0Smatt
104290a34a0Smatt obio_found = 1;
105290a34a0Smatt sc->sc_dev = self;
106290a34a0Smatt
107290a34a0Smatt ba = (bus_addr_t)rmixl_configuration.rc_io_pbase;
108290a34a0Smatt KASSERT(ba != 0);
109290a34a0Smatt
110290a34a0Smatt obio_bus_init(sc);
111290a34a0Smatt
112290a34a0Smatt aprint_normal(" addr %#"PRIxBUSADDR" size %#"PRIxBUSSIZE"\n",
113290a34a0Smatt ba, (bus_size_t)RMIXL_IO_DEV_SIZE);
114290a34a0Smatt aprint_naive("\n");
115290a34a0Smatt
116290a34a0Smatt /*
117290a34a0Smatt * Attach on-board devices as specified in the kernel config file.
118290a34a0Smatt */
1192685996bSthorpej config_search(self, NULL,
120c7fb772bSthorpej CFARGS(.search = obio_search));
121290a34a0Smatt }
122290a34a0Smatt
123290a34a0Smatt static int
obio_print(void * aux,const char * pnp)124290a34a0Smatt obio_print(void *aux, const char *pnp)
125290a34a0Smatt {
126290a34a0Smatt struct obio_attach_args *obio = aux;
127290a34a0Smatt
128290a34a0Smatt if (obio->obio_addr != OBIOCF_ADDR_DEFAULT) {
129290a34a0Smatt aprint_normal(" addr %#"PRIxBUSADDR, obio->obio_addr);
130290a34a0Smatt if (obio->obio_size != OBIOCF_SIZE_DEFAULT)
131290a34a0Smatt aprint_normal("-%#"PRIxBUSADDR,
132290a34a0Smatt obio->obio_addr + (obio->obio_size - 1));
133290a34a0Smatt }
134290a34a0Smatt if (obio->obio_mult != OBIOCF_MULT_DEFAULT)
135290a34a0Smatt aprint_normal(" mult %d", obio->obio_mult);
136290a34a0Smatt if (obio->obio_intr != OBIOCF_INTR_DEFAULT)
137290a34a0Smatt aprint_normal(" intr %d", obio->obio_intr);
1383e67b512Smatt if (obio->obio_tmsk != OBIOCF_TMSK_DEFAULT)
1393e67b512Smatt aprint_normal(" tmsk %d", obio->obio_tmsk);
140290a34a0Smatt
141290a34a0Smatt return (UNCONF);
142290a34a0Smatt }
143290a34a0Smatt
144290a34a0Smatt static int
obio_search(device_t parent,cfdata_t cf,const int * ldesc,void * aux)145290a34a0Smatt obio_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
146290a34a0Smatt {
147290a34a0Smatt struct obio_softc *sc = device_private(parent);
148290a34a0Smatt struct obio_attach_args obio;
149290a34a0Smatt
1503e67b512Smatt obio.obio_eb_bst = sc->sc_eb_bst;
1513e67b512Smatt obio.obio_el_bst = sc->sc_el_bst;
152290a34a0Smatt obio.obio_addr = cf->cf_loc[OBIOCF_ADDR];
153290a34a0Smatt obio.obio_size = cf->cf_loc[OBIOCF_SIZE];
154290a34a0Smatt obio.obio_mult = cf->cf_loc[OBIOCF_MULT];
155290a34a0Smatt obio.obio_intr = cf->cf_loc[OBIOCF_INTR];
1563e67b512Smatt obio.obio_tmsk = cf->cf_loc[OBIOCF_TMSK];
157290a34a0Smatt obio.obio_29bit_dmat = sc->sc_29bit_dmat;
158290a34a0Smatt obio.obio_32bit_dmat = sc->sc_32bit_dmat;
159290a34a0Smatt obio.obio_64bit_dmat = sc->sc_64bit_dmat;
160290a34a0Smatt
1612685996bSthorpej if (config_probe(parent, cf, &obio))
162c7fb772bSthorpej config_attach(parent, cf, &obio, obio_print, CFARGS_NONE);
163290a34a0Smatt
164290a34a0Smatt return 0;
165290a34a0Smatt }
166290a34a0Smatt
167290a34a0Smatt static void
obio_bus_init(struct obio_softc * sc)168290a34a0Smatt obio_bus_init(struct obio_softc *sc)
169290a34a0Smatt {
170290a34a0Smatt struct rmixl_config *rcp = &rmixl_configuration;
171290a34a0Smatt static int done = 0;
1723e67b512Smatt int error;
173290a34a0Smatt
174290a34a0Smatt if (done)
175290a34a0Smatt return;
176290a34a0Smatt done = 1;
177290a34a0Smatt
1783e67b512Smatt /* obio (devio) space, Big Endian */
1793e67b512Smatt if (rcp->rc_obio_eb_memt.bs_cookie == 0)
1803e67b512Smatt rmixl_obio_eb_bus_mem_init(&rcp->rc_obio_eb_memt, rcp);
1813e67b512Smatt
1823e67b512Smatt /* obio (devio) space, Little Endian */
1833e67b512Smatt if (rcp->rc_obio_el_memt.bs_cookie == 0)
1843e67b512Smatt rmixl_obio_el_bus_mem_init(&rcp->rc_obio_el_memt, rcp);
1853e67b512Smatt
1863e67b512Smatt /* dma space for all memory, including >= 4GB */
1873e67b512Smatt if (rcp->rc_dma_tag._cookie == 0)
1883e67b512Smatt obio_dma_init_64(&rcp->rc_dma_tag);
1893e67b512Smatt rcp->rc_64bit_dmat = &rcp->rc_dma_tag;
1903e67b512Smatt
1913e67b512Smatt /* dma space for addr < 4GB */
1923e67b512Smatt if (rcp->rc_32bit_dmat == NULL) {
1933e67b512Smatt error = bus_dmatag_subregion(rcp->rc_64bit_dmat,
194e6a4e4ebSskrll 0, __MASK(32), &rcp->rc_32bit_dmat, 0);
1953e67b512Smatt if (error)
1963e67b512Smatt panic("%s: failed to create 32bit dma tag: %d",
1973e67b512Smatt __func__, error);
1983e67b512Smatt }
199290a34a0Smatt
200290a34a0Smatt /* dma space for addr < 512MB */
2013e67b512Smatt if (rcp->rc_29bit_dmat == NULL) {
2023e67b512Smatt error = bus_dmatag_subregion(rcp->rc_32bit_dmat,
203e6a4e4ebSskrll 0, __MASK(29), &rcp->rc_29bit_dmat, 0);
2043e67b512Smatt if (error)
2053e67b512Smatt panic("%s: failed to create 29bit dma tag: %d",
2063e67b512Smatt __func__, error);
2073e67b512Smatt }
208290a34a0Smatt
209290a34a0Smatt sc->sc_base = (bus_addr_t)rcp->rc_io_pbase;
210290a34a0Smatt sc->sc_size = (bus_size_t)RMIXL_IO_DEV_SIZE;
2113e67b512Smatt sc->sc_eb_bst = (bus_space_tag_t)&rcp->rc_obio_eb_memt;
2123e67b512Smatt sc->sc_el_bst = (bus_space_tag_t)&rcp->rc_obio_el_memt;
2133e67b512Smatt sc->sc_29bit_dmat = rcp->rc_29bit_dmat;
2143e67b512Smatt sc->sc_32bit_dmat = rcp->rc_32bit_dmat;
2153e67b512Smatt sc->sc_64bit_dmat = rcp->rc_64bit_dmat;
216290a34a0Smatt }
217290a34a0Smatt
218290a34a0Smatt static void
obio_dma_init_64(bus_dma_tag_t t)2193e67b512Smatt obio_dma_init_64(bus_dma_tag_t t)
220290a34a0Smatt {
221290a34a0Smatt t->_cookie = t;
222290a34a0Smatt t->_wbase = 0;
2233e67b512Smatt t->_bounce_alloc_lo = 0;
2243e67b512Smatt t->_bounce_alloc_hi = 0;
2253e67b512Smatt t->_dmamap_ops = mips_bus_dmamap_ops;
2263e67b512Smatt t->_dmamem_ops = mips_bus_dmamem_ops;
2273e67b512Smatt t->_dmatag_ops = mips_bus_dmatag_ops;
228290a34a0Smatt }
229290a34a0Smatt
230290a34a0Smatt void
rmixl_addr_error_init(void)231290a34a0Smatt rmixl_addr_error_init(void)
232290a34a0Smatt {
233290a34a0Smatt uint32_t r;
234290a34a0Smatt
235290a34a0Smatt /*
236290a34a0Smatt * activate error addr detection on all (configurable) devices
237290a34a0Smatt * preserve reserved bit fields
238290a34a0Smatt * note some of these bits are read-only (writes are ignored)
239290a34a0Smatt */
240290a34a0Smatt r = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_DEVICE_MASK);
241290a34a0Smatt r |= ~(__BITS(19,16) | __BITS(10,9) | __BITS(7,5));
242290a34a0Smatt RMIXL_IOREG_WRITE(RMIXL_ADDR_ERR_DEVICE_MASK, r);
243290a34a0Smatt
244290a34a0Smatt /*
245290a34a0Smatt * enable the address error interrupts
246290a34a0Smatt * "upgrade" cache and CPU errors to A1
247290a34a0Smatt */
248290a34a0Smatt #define _ADDR_ERR_DEVSTAT_A1 (__BIT(8) | __BIT(1) | __BIT(0))
249290a34a0Smatt #define _ADDR_ERR_RESV \
250290a34a0Smatt (__BITS(31,21) | __BITS(15,14) | __BITS(10,9) | __BITS(7,2))
251290a34a0Smatt #define _BITERR_INT_EN_RESV (__BITS(31,8) | __BIT(4))
252290a34a0Smatt
253290a34a0Smatt r = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_EN);
254290a34a0Smatt r &= _ADDR_ERR_RESV;
255290a34a0Smatt r |= ~_ADDR_ERR_RESV;
256290a34a0Smatt RMIXL_IOREG_WRITE(RMIXL_ADDR_ERR_AERR0_EN, r);
257290a34a0Smatt
258290a34a0Smatt r = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_UPG);
259290a34a0Smatt r &= _ADDR_ERR_RESV;
260290a34a0Smatt r |= _ADDR_ERR_DEVSTAT_A1;
261290a34a0Smatt RMIXL_IOREG_WRITE(RMIXL_ADDR_ERR_AERR0_UPG, r);
262290a34a0Smatt
263290a34a0Smatt /*
264290a34a0Smatt * clear the log regs and the dev stat (interrupt status) regs
265290a34a0Smatt * "Write any value to bit[0] to clear"
266290a34a0Smatt */
267290a34a0Smatt r = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR1_CLEAR);
268290a34a0Smatt RMIXL_IOREG_WRITE(RMIXL_ADDR_ERR_AERR1_CLEAR, r);
269290a34a0Smatt
270290a34a0Smatt /*
271290a34a0Smatt * enable the double bit error interrupts
272290a34a0Smatt * (assume reserved bits, which are read-only, are ignored)
273290a34a0Smatt */
274290a34a0Smatt r = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_BITERR_INT_EN);
275290a34a0Smatt r &= _BITERR_INT_EN_RESV;
276290a34a0Smatt r |= __BITS(7,5);
277290a34a0Smatt RMIXL_IOREG_WRITE(RMIXL_ADDR_ERR_BITERR_INT_EN, r);
278290a34a0Smatt
279290a34a0Smatt /*
280290a34a0Smatt * establish address error ISR
2813e67b512Smatt * XXX assuming "int 16 (bridge_tb)" is our irq
2823e67b512Smatt * XXX is true for XLS family only
283290a34a0Smatt */
2843e67b512Smatt if (cpu_rmixls(mips_options.mips_cpu))
2853e67b512Smatt rmixl_intr_establish(16, 1, IPL_HIGH,
2863e67b512Smatt RMIXL_TRIG_LEVEL, RMIXL_POLR_HIGH,
2873e67b512Smatt rmixl_addr_error_intr, NULL, false);
288290a34a0Smatt }
289290a34a0Smatt
290290a34a0Smatt int
rmixl_addr_error_check(void)291290a34a0Smatt rmixl_addr_error_check(void)
292290a34a0Smatt {
293290a34a0Smatt uint32_t aerr0_devstat;
294290a34a0Smatt uint32_t aerr0_log1;
295290a34a0Smatt uint32_t aerr0_log2;
296290a34a0Smatt uint32_t aerr0_log3;
297290a34a0Smatt uint32_t aerr1_devstat;
298290a34a0Smatt uint32_t aerr1_log1;
299290a34a0Smatt uint32_t aerr1_log2;
300290a34a0Smatt uint32_t aerr1_log3;
301290a34a0Smatt uint32_t sbe_counts;
302290a34a0Smatt uint32_t dbe_counts;
303290a34a0Smatt
304290a34a0Smatt aerr0_devstat = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_DEVSTAT);
305290a34a0Smatt aerr0_log1 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_LOG1);
306290a34a0Smatt aerr0_log2 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_LOG2);
307290a34a0Smatt aerr0_log3 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR0_LOG3);
308290a34a0Smatt
309290a34a0Smatt aerr1_devstat = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR1_DEVSTAT);
310290a34a0Smatt aerr1_log1 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR1_LOG1);
311290a34a0Smatt aerr1_log2 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR1_LOG2);
312290a34a0Smatt aerr1_log3 = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_AERR1_LOG3);
313290a34a0Smatt
314290a34a0Smatt sbe_counts = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_SBE_COUNTS);
315290a34a0Smatt dbe_counts = RMIXL_IOREG_READ(RMIXL_ADDR_ERR_DBE_COUNTS);
316290a34a0Smatt
317290a34a0Smatt if (aerr0_log1|aerr0_log2|aerr0_log3
318290a34a0Smatt |aerr1_log1|aerr1_log2|aerr1_log3
319290a34a0Smatt |dbe_counts) {
320290a34a0Smatt printf("aerr0: stat %#x, logs: %#x, %#x, %#x\n",
321290a34a0Smatt aerr0_devstat, aerr0_log1, aerr0_log2, aerr0_log2);
322290a34a0Smatt printf("aerr1: stat %#x, logs: %#x, %#x, %#x\n",
323290a34a0Smatt aerr1_devstat, aerr1_log1, aerr1_log2, aerr1_log2);
324290a34a0Smatt printf("1-bit errors: %#x, 2-bit errors: %#x\n",
325290a34a0Smatt sbe_counts, dbe_counts);
326290a34a0Smatt return 1;
327290a34a0Smatt }
328290a34a0Smatt return 0;
329290a34a0Smatt }
330290a34a0Smatt
331290a34a0Smatt static int
rmixl_addr_error_intr(void * arg)332290a34a0Smatt rmixl_addr_error_intr(void *arg)
333290a34a0Smatt {
334290a34a0Smatt int err;
335290a34a0Smatt
336290a34a0Smatt err = rmixl_addr_error_check();
337290a34a0Smatt if (err != 0) {
338290a34a0Smatt #if DDB
339290a34a0Smatt printf("%s\n", __func__);
340290a34a0Smatt Debugger();
341290a34a0Smatt #endif
342290a34a0Smatt panic("Address Error");
343290a34a0Smatt }
344290a34a0Smatt return 1;
345290a34a0Smatt }
346