xref: /openbsd/sys/arch/sparc64/dev/fhc.c (revision 8a1e8cca)
1 /*	$OpenBSD: fhc.c,v 1.22 2024/03/29 21:29:33 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/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/device.h>
33 #include <sys/conf.h>
34 #include <sys/timeout.h>
35 #include <sys/malloc.h>
36 
37 #include <machine/bus.h>
38 #include <machine/autoconf.h>
39 #include <machine/openfirm.h>
40 
41 #include <sparc64/dev/fhcreg.h>
42 #include <sparc64/dev/fhcvar.h>
43 #include <sparc64/dev/iommureg.h>
44 
45 struct cfdriver fhc_cd = {
46 	NULL, "fhc", DV_DULL
47 };
48 
49 int	fhc_print(void *, const char *);
50 
51 bus_space_tag_t fhc_alloc_bus_tag(struct fhc_softc *);
52 int _fhc_bus_map(bus_space_tag_t, bus_space_tag_t, bus_addr_t, bus_size_t,
53     int, bus_space_handle_t *);
54 void *fhc_intr_establish(bus_space_tag_t, bus_space_tag_t, int, int, int,
55     int (*)(void *), void *, const char *);
56 bus_space_handle_t *fhc_find_intr_handle(struct fhc_softc *, int);
57 void fhc_led_blink(void *, int);
58 
59 void
fhc_attach(struct fhc_softc * sc)60 fhc_attach(struct fhc_softc *sc)
61 {
62 	int node0, node;
63 	u_int32_t ctrl;
64 
65 	printf(" board %d: %s\n", sc->sc_board,
66 	    getpropstring(sc->sc_node, "board-model"));
67 
68 	sc->sc_cbt = fhc_alloc_bus_tag(sc);
69 
70 	sc->sc_ign = sc->sc_board << 1;
71 	bus_space_write_4(sc->sc_bt, sc->sc_ireg, FHC_I_IGN, sc->sc_ign);
72 	sc->sc_ign = bus_space_read_4(sc->sc_bt, sc->sc_ireg, FHC_I_IGN);
73 
74 	ctrl = bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL);
75 	if (!sc->sc_is_central)
76 		ctrl |= FHC_P_CTRL_IXIST;
77 	ctrl &= ~(FHC_P_CTRL_AOFF | FHC_P_CTRL_BOFF | FHC_P_CTRL_SLINE);
78 	bus_space_write_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL, ctrl);
79 	bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL);
80 
81 	/* clear interrupts */
82 	bus_space_write_4(sc->sc_bt, sc->sc_freg, FHC_F_ICLR, 0);
83 	bus_space_read_4(sc->sc_bt, sc->sc_freg, FHC_F_ICLR);
84 	bus_space_write_4(sc->sc_bt, sc->sc_sreg, FHC_S_ICLR, 0);
85 	bus_space_read_4(sc->sc_bt, sc->sc_sreg, FHC_S_ICLR);
86 	bus_space_write_4(sc->sc_bt, sc->sc_ureg, FHC_U_ICLR, 0);
87 	bus_space_read_4(sc->sc_bt, sc->sc_ureg, FHC_U_ICLR);
88 	bus_space_write_4(sc->sc_bt, sc->sc_treg, FHC_T_ICLR, 0);
89 	bus_space_read_4(sc->sc_bt, sc->sc_treg, FHC_T_ICLR);
90 
91 	getprop(sc->sc_node, "ranges", sizeof(struct fhc_range),
92 	    &sc->sc_nrange, (void **)&sc->sc_range);
93 
94 	node0 = firstchild(sc->sc_node);
95 	for (node = node0; node; node = nextsibling(node)) {
96 		struct fhc_attach_args fa;
97 
98 		if (!checkstatus(node))
99 			continue;
100 
101 		bzero(&fa, sizeof(fa));
102 
103 		fa.fa_node = node;
104 		fa.fa_bustag = sc->sc_cbt;
105 
106 		if (fhc_get_string(fa.fa_node, "name", &fa.fa_name)) {
107 			printf("can't fetch name for node 0x%x\n", node);
108 			continue;
109 		}
110 		getprop(node, "reg", sizeof(struct fhc_reg),
111 		    &fa.fa_nreg, (void **)&fa.fa_reg);
112 		getprop(node, "interrupts", sizeof(int),
113 		    &fa.fa_nintr, (void **)&fa.fa_intr);
114 		getprop(node, "address", sizeof(*fa.fa_promvaddrs),
115 		    &fa.fa_npromvaddrs, (void **)&fa.fa_promvaddrs);
116 
117 		(void)config_found(&sc->sc_dv, (void *)&fa, fhc_print);
118 
119 		free(fa.fa_name, M_DEVBUF, 0);
120 		free(fa.fa_reg, M_DEVBUF, 0);
121 		free(fa.fa_intr, M_DEVBUF, 0);
122 		free(fa.fa_promvaddrs, M_DEVBUF, 0);
123 	}
124 
125 	sc->sc_blink.bl_func = fhc_led_blink;
126 	sc->sc_blink.bl_arg = sc;
127 	blink_led_register(&sc->sc_blink);
128 }
129 
130 int
fhc_print(void * args,const char * busname)131 fhc_print(void *args, const char *busname)
132 {
133 	struct fhc_attach_args *fa = args;
134 	char *class;
135 
136 	if (busname != NULL) {
137 		printf("\"%s\" at %s", fa->fa_name, busname);
138 		class = getpropstring(fa->fa_node, "device_type");
139 		if (*class != '\0')
140 			printf(" class %s", class);
141 	}
142 	return (UNCONF);
143 }
144 
145 int
fhc_get_string(int node,char * name,char ** buf)146 fhc_get_string(int node, char *name, char **buf)
147 {
148 	int len;
149 
150 	len = getproplen(node, name);
151 	if (len < 0)
152 		return (len);
153 	*buf = (char *)malloc(len + 1, M_DEVBUF, M_NOWAIT);
154 	if (*buf == NULL)
155 		return (-1);
156 
157 	if (len != 0)
158 		getpropstringA(node, name, *buf);
159 	(*buf)[len] = '\0';
160 	return (0);
161 }
162 
163 bus_space_tag_t
fhc_alloc_bus_tag(struct fhc_softc * sc)164 fhc_alloc_bus_tag(struct fhc_softc *sc)
165 {
166 	struct sparc_bus_space_tag *bt;
167 
168 	bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
169 	if (bt == NULL)
170 		panic("fhc: couldn't alloc bus tag");
171 
172 	strlcpy(bt->name, sc->sc_dv.dv_xname, sizeof(bt->name));
173 	bt->cookie = sc;
174 	bt->parent = sc->sc_bt;
175 	bt->asi = bt->parent->asi;
176 	bt->sasi = bt->parent->sasi;
177 	bt->sparc_bus_map = _fhc_bus_map;
178 	/* XXX bt->sparc_bus_mmap = fhc_bus_mmap; */
179 	bt->sparc_intr_establish = fhc_intr_establish;
180 	return (bt);
181 }
182 
183 int
_fhc_bus_map(bus_space_tag_t t,bus_space_tag_t t0,bus_addr_t addr,bus_size_t size,int flags,bus_space_handle_t * hp)184 _fhc_bus_map(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t addr,
185     bus_size_t size, int flags, bus_space_handle_t *hp)
186 {
187 	struct fhc_softc *sc = t->cookie;
188 	int64_t slot = BUS_ADDR_IOSPACE(addr);
189 	int64_t offset = BUS_ADDR_PADDR(addr);
190 	int i;
191 
192 	if (t->parent == NULL || t->parent->sparc_bus_map == NULL) {
193 		printf("\n_fhc_bus_map: invalid parent");
194 		return (EINVAL);
195 	}
196 
197 	if (flags & BUS_SPACE_MAP_PROMADDRESS)
198 		return ((*t->parent->sparc_bus_map)(t, t0, addr,
199 		    size, flags, hp));
200 
201 	for (i = 0; i < sc->sc_nrange; i++) {
202 		bus_addr_t paddr;
203 
204 		if (sc->sc_range[i].cspace != slot)
205 			continue;
206 
207 		paddr = offset - sc->sc_range[i].coffset;
208 		paddr += sc->sc_range[i].poffset;
209 		paddr |= ((bus_addr_t)sc->sc_range[i].pspace << 32);
210 
211 		return ((*t->parent->sparc_bus_map)(t->parent, t0, paddr,
212 		    size, flags, hp));
213 	}
214 
215 	return (EINVAL);
216 }
217 
218 bus_space_handle_t *
fhc_find_intr_handle(struct fhc_softc * sc,int ino)219 fhc_find_intr_handle(struct fhc_softc *sc, int ino)
220 {
221 	switch (FHC_INO(ino)) {
222 	case FHC_F_INO:
223 		return &sc->sc_freg;
224 	case FHC_S_INO:
225 		return &sc->sc_sreg;
226 	case FHC_U_INO:
227 		return &sc->sc_ureg;
228 	case FHC_T_INO:
229 		return &sc->sc_treg;
230 	default:
231 		break;
232 	}
233 
234 	return (NULL);
235 }
236 
237 void *
fhc_intr_establish(bus_space_tag_t t,bus_space_tag_t t0,int ihandle,int level,int flags,int (* handler)(void *),void * arg,const char * what)238 fhc_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle,
239     int level, int flags, int (*handler)(void *), void *arg, const char *what)
240 {
241 	struct fhc_softc *sc = t->cookie;
242 	volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
243 	struct intrhand *ih;
244 	long vec;
245 
246 	if (level == IPL_NONE)
247 		level = INTLEV(ihandle);
248 	if (level == IPL_NONE) {
249 		printf(": no IPL, setting IPL 2.\n");
250 		level = 2;
251 	}
252 
253 	if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
254 		bus_space_handle_t *hp;
255 		struct fhc_intr_reg *intrregs;
256 
257 		hp = fhc_find_intr_handle(sc, ihandle);
258 		if (hp == NULL) {
259 			printf(": can't find intr handle\n");
260 			return (NULL);
261 		}
262 
263 		intrregs = bus_space_vaddr(sc->sc_bt, *hp);
264 		intrmapptr = &intrregs->imap;
265 		intrclrptr = &intrregs->iclr;
266 		vec = ((sc->sc_ign << INTMAP_IGN_SHIFT) & INTMAP_IGN) |
267 		    INTINO(ihandle);
268 	} else
269 		vec = INTVEC(ihandle);
270 
271 	ih = bus_intr_allocate(t0, handler, arg, vec, level, intrmapptr,
272 	    intrclrptr, what);
273 	if (ih == NULL)
274 		return (NULL);
275 
276 	intr_establish(ih);
277 
278 	if (intrmapptr != NULL) {
279 		u_int64_t r;
280 
281 		r = *intrmapptr;
282 		r |= INTMAP_V;
283 		*intrmapptr = r;
284 		r = *intrmapptr;
285 		ih->ih_number |= r & INTMAP_INR;
286 	}
287 
288 	return (ih);
289 }
290 
291 void
fhc_led_blink(void * vsc,int on)292 fhc_led_blink(void *vsc, int on)
293 {
294 	struct fhc_softc *sc = vsc;
295 	int s;
296 	u_int32_t r;
297 
298 	s = splhigh();
299 	r = bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL);
300 	if (on)
301 		r |= FHC_P_CTRL_RLED;
302 	else
303 		r &= ~FHC_P_CTRL_RLED;
304 	r &= ~(FHC_P_CTRL_AOFF | FHC_P_CTRL_BOFF | FHC_P_CTRL_SLINE);
305 	bus_space_write_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL, r);
306 	bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL);
307 	splx(s);
308 }
309