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