fhc.c revision 1.6.8.1 1 /* $NetBSD: fhc.c,v 1.6.8.1 2021/04/03 22:28:38 thorpej 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 (at) 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/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: fhc.c,v 1.6.8.1 2021/04/03 22:28:38 thorpej Exp $");
32
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/conf.h>
39 #include <sys/malloc.h>
40 #include <sys/kmem.h>
41 #include <sys/bus.h>
42
43 #include <machine/autoconf.h>
44 #include <machine/openfirm.h>
45
46 #include <sparc64/dev/fhcreg.h>
47 #include <sparc64/dev/fhcvar.h>
48 #include <sparc64/dev/iommureg.h>
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_addr_t, bus_size_t, int, vaddr_t,
54 bus_space_handle_t *);
55 void *fhc_intr_establish(bus_space_tag_t, int, int,
56 int (*)(void *), void *, void (*)(void));
57 bus_space_handle_t *fhc_find_intr_handle(struct fhc_softc *, int);
58
59 void
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 prom_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 prom_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 0
99 if (!checkstatus(node))
100 continue;
101 #endif
102
103 bzero(&fa, sizeof(fa));
104
105 fa.fa_node = node;
106 fa.fa_bustag = sc->sc_cbt;
107
108 if (fhc_get_string(fa.fa_node, "name", &fa.fa_name)) {
109 printf("can't fetch name for node 0x%x\n", node);
110 continue;
111 }
112 prom_getprop(node, "reg", sizeof(struct fhc_reg),
113 &fa.fa_nreg, (void **)&fa.fa_reg);
114 prom_getprop(node, "interrupts", sizeof(int),
115 &fa.fa_nintr, (void **)&fa.fa_intr);
116 prom_getprop(node, "address", sizeof(*fa.fa_promvaddrs),
117 &fa.fa_npromvaddrs, (void **)&fa.fa_promvaddrs);
118
119 (void)config_found(sc->sc_dev, (void *)&fa, fhc_print);
120
121 if (fa.fa_name != NULL)
122 free(fa.fa_name, M_DEVBUF);
123 if (fa.fa_reg != NULL)
124 free(fa.fa_reg, M_DEVBUF);
125 if (fa.fa_intr != NULL)
126 free(fa.fa_intr, M_DEVBUF);
127 if (fa.fa_promvaddrs != NULL)
128 free(fa.fa_promvaddrs, M_DEVBUF);
129 }
130 }
131
132 int
133 fhc_print(void *args, const char *busname)
134 {
135 struct fhc_attach_args *fa = args;
136 char *class;
137
138 if (busname != NULL) {
139 printf("\"%s\" at %s", fa->fa_name, busname);
140 class = prom_getpropstring(fa->fa_node, "device_type");
141 if (*class != '\0')
142 printf(" class %s", class);
143 }
144 return (UNCONF);
145 }
146
147 int
148 fhc_get_string(int node, const char *name, char **buf)
149 {
150 int len;
151
152 len = prom_getproplen(node, name);
153 if (len < 0)
154 return (len);
155 *buf = (char *)malloc(len + 1, M_DEVBUF, M_WAITOK);
156 if (len != 0)
157 prom_getpropstringA(node, name, *buf, len + 1);
158 (*buf)[len] = '\0';
159 return (0);
160 }
161
162 bus_space_tag_t
163 fhc_alloc_bus_tag(struct fhc_softc *sc)
164 {
165 struct sparc_bus_space_tag *bt;
166
167 bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
168 bt->cookie = sc;
169 bt->parent = sc->sc_bt;
170 bt->type = 0; /* XXX asi? */
171 bt->sparc_bus_map = _fhc_bus_map;
172 /* XXX bt->sparc_bus_mmap = fhc_bus_mmap; */
173 bt->sparc_intr_establish = fhc_intr_establish;
174 return (bt);
175 }
176
177 int
178 _fhc_bus_map(bus_space_tag_t t, bus_addr_t addr, bus_size_t size,
179 int flags, vaddr_t unused, bus_space_handle_t *hp)
180 {
181 struct fhc_softc *sc = t->cookie;
182 int64_t slot = BUS_ADDR_IOSPACE(addr);
183 int64_t offset = BUS_ADDR_PADDR(addr);
184 int i;
185
186 if (t->parent == NULL || t->parent->sparc_bus_map == NULL) {
187 printf("\n_fhc_bus_map: invalid parent");
188 return (EINVAL);
189 }
190
191 for (i = 0; i < sc->sc_nrange; i++) {
192 bus_addr_t paddr;
193
194 if (sc->sc_range[i].cspace != slot)
195 continue;
196
197 paddr = offset - sc->sc_range[i].coffset;
198 paddr += sc->sc_range[i].poffset;
199 paddr |= ((bus_addr_t)sc->sc_range[i].pspace << 32);
200
201 return bus_space_map(t->parent, paddr, size, flags, hp);
202 }
203
204 return (EINVAL);
205 }
206
207 bus_space_handle_t *
208 fhc_find_intr_handle(struct fhc_softc *sc, int ino)
209 {
210 switch (FHC_INO(ino)) {
211 case FHC_F_INO:
212 return &sc->sc_freg;
213 case FHC_S_INO:
214 return &sc->sc_sreg;
215 case FHC_U_INO:
216 return &sc->sc_ureg;
217 case FHC_T_INO:
218 return &sc->sc_treg;
219 default:
220 break;
221 }
222
223 return (NULL);
224 }
225
226 void *
227 fhc_intr_establish(bus_space_tag_t t, int ihandle, int level,
228 int (*handler)(void *), void *arg, void (*fastvec)(void) /* ignored */)
229 {
230 struct fhc_softc *sc = t->cookie;
231 volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
232 struct intrhand *ih;
233 long vec;
234 bus_space_handle_t *hp;
235 struct fhc_intr_reg *intrregs;
236
237 hp = fhc_find_intr_handle(sc, ihandle);
238 if (hp == NULL) {
239 printf(": can't find intr handle\n");
240 return (NULL);
241 }
242
243 intrregs = bus_space_vaddr(sc->sc_bt, *hp);
244 intrmapptr = &intrregs->imap;
245 intrclrptr = &intrregs->iclr;
246 vec = ((sc->sc_ign << INTMAP_IGN_SHIFT) & INTMAP_IGN) |
247 INTINO(ihandle);
248
249 ih = intrhand_alloc();
250
251 ih->ih_ivec = ihandle;
252
253 /* Register the map and clear intr registers */
254 ih->ih_map = intrmapptr;
255 ih->ih_clr = intrclrptr;
256
257 ih->ih_fun = handler;
258 ih->ih_arg = arg;
259 ih->ih_pil = level;
260 ih->ih_number = vec;
261
262 intr_establish(ih->ih_pil, level != IPL_VM, ih);
263
264 /*
265 * XXXX --- we really should use bus_space for this.
266 */
267 if (intrmapptr != NULL) {
268 u_int64_t r;
269
270 r = *intrmapptr;
271 r |= INTMAP_V | (CPU_UPAID << INTMAP_TID_SHIFT);
272 *intrmapptr = r;
273 r = *intrmapptr;
274 ih->ih_number |= r & INTMAP_INR;
275 }
276
277 return (ih);
278 }
279