xref: /netbsd-src/sys/arch/sparc64/dev/fhc.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: fhc.c,v 1.2 2011/08/12 08:24:01 jdc Exp $	*/
2 /*	$OpenBSD: fhc.c,v 1.17 2010/11/11 17:58:23 miod Exp $	*/
3 
4 /*
5  * Copyright (c) 2004 Jason L. Wright (jason@thought.net)
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/device.h>
35 #include <sys/conf.h>
36 #include <sys/malloc.h>
37 #include <sys/bus.h>
38 
39 #include <machine/autoconf.h>
40 #include <machine/openfirm.h>
41 
42 #include <sparc64/dev/fhcreg.h>
43 #include <sparc64/dev/fhcvar.h>
44 #include <sparc64/dev/iommureg.h>
45 
46 int	fhc_print(void *, const char *);
47 
48 bus_space_tag_t fhc_alloc_bus_tag(struct fhc_softc *);
49 int _fhc_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int, vaddr_t,
50     bus_space_handle_t *);
51 void *fhc_intr_establish(bus_space_tag_t, int, int,
52     int (*)(void *), void *, void (*)(void));
53 bus_space_handle_t *fhc_find_intr_handle(struct fhc_softc *, int);
54 
55 void
56 fhc_attach(struct fhc_softc *sc)
57 {
58 	int node0, node;
59 	u_int32_t ctrl;
60 
61 	printf(" board %d: %s\n", sc->sc_board,
62 	    prom_getpropstring(sc->sc_node, "board-model"));
63 
64 	sc->sc_cbt = fhc_alloc_bus_tag(sc);
65 
66 	sc->sc_ign = sc->sc_board << 1;
67 	bus_space_write_4(sc->sc_bt, sc->sc_ireg, FHC_I_IGN, sc->sc_ign);
68 	sc->sc_ign = bus_space_read_4(sc->sc_bt, sc->sc_ireg, FHC_I_IGN);
69 
70 	ctrl = bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL);
71 	if (!sc->sc_is_central)
72 		ctrl |= FHC_P_CTRL_IXIST;
73 	ctrl &= ~(FHC_P_CTRL_AOFF | FHC_P_CTRL_BOFF | FHC_P_CTRL_SLINE);
74 	bus_space_write_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL, ctrl);
75 	bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL);
76 
77 	/* clear interrupts */
78 	bus_space_write_4(sc->sc_bt, sc->sc_freg, FHC_F_ICLR, 0);
79 	bus_space_read_4(sc->sc_bt, sc->sc_freg, FHC_F_ICLR);
80 	bus_space_write_4(sc->sc_bt, sc->sc_sreg, FHC_S_ICLR, 0);
81 	bus_space_read_4(sc->sc_bt, sc->sc_sreg, FHC_S_ICLR);
82 	bus_space_write_4(sc->sc_bt, sc->sc_ureg, FHC_U_ICLR, 0);
83 	bus_space_read_4(sc->sc_bt, sc->sc_ureg, FHC_U_ICLR);
84 	bus_space_write_4(sc->sc_bt, sc->sc_treg, FHC_T_ICLR, 0);
85 	bus_space_read_4(sc->sc_bt, sc->sc_treg, FHC_T_ICLR);
86 
87 	prom_getprop(sc->sc_node, "ranges", sizeof(struct fhc_range),
88 	    &sc->sc_nrange, (void **)&sc->sc_range);
89 
90 	node0 = firstchild(sc->sc_node);
91 	for (node = node0; node; node = nextsibling(node)) {
92 		struct fhc_attach_args fa;
93 
94 #if 0
95 		if (!checkstatus(node))
96 			continue;
97 #endif
98 
99 		bzero(&fa, sizeof(fa));
100 
101 		fa.fa_node = node;
102 		fa.fa_bustag = sc->sc_cbt;
103 
104 		if (fhc_get_string(fa.fa_node, "name", &fa.fa_name)) {
105 			printf("can't fetch name for node 0x%x\n", node);
106 			continue;
107 		}
108 		prom_getprop(node, "reg", sizeof(struct fhc_reg),
109 		    &fa.fa_nreg, (void **)&fa.fa_reg);
110 		prom_getprop(node, "interrupts", sizeof(int),
111 		    &fa.fa_nintr, (void **)&fa.fa_intr);
112 		prom_getprop(node, "address", sizeof(*fa.fa_promvaddrs),
113 		    &fa.fa_npromvaddrs, (void **)&fa.fa_promvaddrs);
114 
115 		(void)config_found(sc->sc_dev, (void *)&fa, fhc_print);
116 
117 		if (fa.fa_name != NULL)
118 			free(fa.fa_name, M_DEVBUF);
119 		if (fa.fa_reg != NULL)
120 			free(fa.fa_reg, M_DEVBUF);
121 		if (fa.fa_intr != NULL)
122 			free(fa.fa_intr, M_DEVBUF);
123 		if (fa.fa_promvaddrs != NULL)
124 			free(fa.fa_promvaddrs, M_DEVBUF);
125 	}
126 }
127 
128 int
129 fhc_print(void *args, const char *busname)
130 {
131 	struct fhc_attach_args *fa = args;
132 	char *class;
133 
134 	if (busname != NULL) {
135 		printf("\"%s\" at %s", fa->fa_name, busname);
136 		class = prom_getpropstring(fa->fa_node, "device_type");
137 		if (*class != '\0')
138 			printf(" class %s", class);
139 	}
140 	return (UNCONF);
141 }
142 
143 int
144 fhc_get_string(int node, const char *name, char **buf)
145 {
146 	int len;
147 
148 	len = prom_getproplen(node, name);
149 	if (len < 0)
150 		return (len);
151 	*buf = (char *)malloc(len + 1, M_DEVBUF, M_NOWAIT);
152 	if (*buf == NULL)
153 		return (-1);
154 
155 	if (len != 0)
156 		prom_getpropstringA(node, name, *buf, len + 1);
157 	(*buf)[len] = '\0';
158 	return (0);
159 }
160 
161 bus_space_tag_t
162 fhc_alloc_bus_tag(struct fhc_softc *sc)
163 {
164 	struct sparc_bus_space_tag *bt;
165 
166 	bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
167 	if (bt == NULL)
168 		panic("fhc: couldn't alloc bus tag");
169 
170 	bt->cookie = sc;
171 	bt->parent = sc->sc_bt;
172 	bt->type = 0;	/* XXX asi? */
173 	bt->sparc_bus_map = _fhc_bus_map;
174 	/* XXX bt->sparc_bus_mmap = fhc_bus_mmap; */
175 	bt->sparc_intr_establish = fhc_intr_establish;
176 	return (bt);
177 }
178 
179 int
180 _fhc_bus_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size,
181 	     int flags, vaddr_t unused, bus_space_handle_t *hp)
182 {
183 	struct fhc_softc *sc = t->cookie;
184 	int64_t slot = BUS_ADDR_IOSPACE(addr);
185 	int64_t offset = BUS_ADDR_PADDR(addr);
186 	int i;
187 
188 	if (t->parent == NULL || t->parent->sparc_bus_map == NULL) {
189 		printf("\n_fhc_bus_map: invalid parent");
190 		return (EINVAL);
191 	}
192 
193 	for (i = 0; i < sc->sc_nrange; i++) {
194 		bus_addr_t paddr;
195 
196 		if (sc->sc_range[i].cspace != slot)
197 			continue;
198 
199 		paddr = offset - sc->sc_range[i].coffset;
200 		paddr += sc->sc_range[i].poffset;
201 		paddr |= ((bus_addr_t)sc->sc_range[i].pspace << 32);
202 
203 		return bus_space_map(t->parent, paddr, size, flags, hp);
204 	}
205 
206 	return (EINVAL);
207 }
208 
209 bus_space_handle_t *
210 fhc_find_intr_handle(struct fhc_softc *sc, int ino)
211 {
212 	switch (FHC_INO(ino)) {
213 	case FHC_F_INO:
214 		return &sc->sc_freg;
215 	case FHC_S_INO:
216 		return &sc->sc_sreg;
217 	case FHC_U_INO:
218 		return &sc->sc_ureg;
219 	case FHC_T_INO:
220 		return &sc->sc_treg;
221 	default:
222 		break;
223 	}
224 
225 	return (NULL);
226 }
227 
228 void *
229 fhc_intr_establish(bus_space_tag_t t, int ihandle, int level,
230 	int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
231 {
232 	struct fhc_softc *sc = t->cookie;
233 	volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
234 	struct intrhand *ih;
235 	long vec;
236 	bus_space_handle_t *hp;
237 	struct fhc_intr_reg *intrregs;
238 
239 	hp = fhc_find_intr_handle(sc, ihandle);
240 	if (hp == NULL) {
241 		printf(": can't find intr handle\n");
242 		return (NULL);
243 	}
244 
245 	intrregs = bus_space_vaddr(sc->sc_bt, *hp);
246 	intrmapptr = &intrregs->imap;
247 	intrclrptr = &intrregs->iclr;
248 	vec = ((sc->sc_ign << INTMAP_IGN_SHIFT) & INTMAP_IGN) |
249 	    INTINO(ihandle);
250 
251 	ih = (struct intrhand *)
252 		malloc(sizeof(struct intrhand), M_DEVBUF, M_NOWAIT);
253 	if (ih == NULL)
254 		return (NULL);
255 
256 	ih->ih_ivec = ihandle;
257 
258 	/* Register the map and clear intr registers */
259 	ih->ih_map = intrmapptr;
260 	ih->ih_clr = intrclrptr;
261 
262 	ih->ih_fun = handler;
263 	ih->ih_arg = arg;
264 	ih->ih_pil = level;
265 	ih->ih_number = vec;
266 
267 	intr_establish(ih->ih_pil, level != IPL_VM, ih);
268 
269 	/*
270 	 * XXXX --- we really should use bus_space for this.
271 	 */
272 	if (intrmapptr != NULL) {
273 		u_int64_t r;
274 
275 		r = *intrmapptr;
276 		r |= INTMAP_V | (CPU_UPAID << INTMAP_TID_SHIFT);
277 		*intrmapptr = r;
278 		r = *intrmapptr;
279 		ih->ih_number |= r & INTMAP_INR;
280 	}
281 
282 	return (ih);
283 }
284