Annotation of sys/arch/sparc64/dev/fhc.c, Revision 1.1.1.1
1.1 nbrk 1: /* $OpenBSD: fhc.c,v 1.13 2007/05/01 19:44:56 kettenis 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: 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: getprop(node, "reg", sizeof(struct fhc_reg),
109: &fa.fa_nreg, (void **)&fa.fa_reg);
110: getprop(node, "interrupts", sizeof(int),
111: &fa.fa_nintr, (void **)&fa.fa_intr);
112: getprop(node, "address", sizeof(*fa.fa_promvaddrs),
113: &fa.fa_npromvaddrs, (void **)&fa.fa_promvaddrs);
114:
115: (void)config_found(&sc->sc_dv, (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_nintr != NULL)
122: free(fa.fa_intr, M_DEVBUF);
123: if (fa.fa_promvaddrs != NULL)
124: free(fa.fa_promvaddrs, M_DEVBUF);
125: }
126:
127: sc->sc_blink.bl_func = fhc_led_blink;
128: sc->sc_blink.bl_arg = sc;
129: blink_led_register(&sc->sc_blink);
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 = 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, char *name, char **buf)
149: {
150: int len;
151:
152: len = getproplen(node, name);
153: if (len < 0)
154: return (len);
155: *buf = (char *)malloc(len + 1, M_DEVBUF, M_NOWAIT);
156: if (*buf == NULL)
157: return (-1);
158:
159: if (len != 0)
160: getpropstringA(node, name, *buf);
161: (*buf)[len] = '\0';
162: return (0);
163: }
164:
165: bus_space_tag_t
166: fhc_alloc_bus_tag(struct fhc_softc *sc)
167: {
168: struct sparc_bus_space_tag *bt;
169:
170: bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT);
171: if (bt == NULL)
172: panic("fhc: couldn't alloc bus tag");
173:
174: bzero(bt, sizeof(*bt));
175: snprintf(bt->name, sizeof(bt->name), "%s", sc->sc_dv.dv_xname);
176: bt->cookie = sc;
177: bt->parent = sc->sc_bt;
178: bt->asi = bt->parent->asi;
179: bt->sasi = bt->parent->sasi;
180: bt->sparc_bus_map = _fhc_bus_map;
181: /* XXX bt->sparc_bus_mmap = fhc_bus_mmap; */
182: bt->sparc_intr_establish = fhc_intr_establish;
183: return (bt);
184: }
185:
186: int
187: _fhc_bus_map(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t addr,
188: bus_size_t size, int flags, bus_space_handle_t *hp)
189: {
190: struct fhc_softc *sc = t->cookie;
191: int64_t slot = BUS_ADDR_IOSPACE(addr);
192: int64_t offset = BUS_ADDR_PADDR(addr);
193: int i;
194:
195: if (t->parent == NULL || t->parent->sparc_bus_map == NULL) {
196: printf("\n_fhc_bus_map: invalid parent");
197: return (EINVAL);
198: }
199:
200: if (flags & BUS_SPACE_MAP_PROMADDRESS)
201: return ((*t->parent->sparc_bus_map)(t, t0, addr,
202: size, flags, hp));
203:
204: for (i = 0; i < sc->sc_nrange; i++) {
205: bus_addr_t paddr;
206:
207: if (sc->sc_range[i].cspace != slot)
208: continue;
209:
210: paddr = offset - sc->sc_range[i].coffset;
211: paddr += sc->sc_range[i].poffset;
212: paddr |= ((bus_addr_t)sc->sc_range[i].pspace << 32);
213:
214: return ((*t->parent->sparc_bus_map)(t->parent, t0, paddr,
215: size, flags, hp));
216: }
217:
218: return (EINVAL);
219: }
220:
221: bus_space_handle_t *
222: fhc_find_intr_handle(struct fhc_softc *sc, int ino)
223: {
224: switch (FHC_INO(ino)) {
225: case FHC_F_INO:
226: return &sc->sc_freg;
227: case FHC_S_INO:
228: return &sc->sc_sreg;
229: case FHC_U_INO:
230: return &sc->sc_ureg;
231: case FHC_T_INO:
232: return &sc->sc_treg;
233: default:
234: break;
235: }
236:
237: return (NULL);
238: }
239:
240: void *
241: fhc_intr_establish(bus_space_tag_t t, bus_space_tag_t t0, int ihandle,
242: int level, int flags, int (*handler)(void *), void *arg, const char *what)
243: {
244: struct fhc_softc *sc = t->cookie;
245: volatile u_int64_t *intrmapptr = NULL, *intrclrptr = NULL;
246: struct intrhand *ih;
247: long vec;
248:
249: if (level == IPL_NONE)
250: level = INTLEV(ihandle);
251: if (level == IPL_NONE) {
252: printf(": no IPL, setting IPL 2.\n");
253: level = 2;
254: }
255:
256: if ((flags & BUS_INTR_ESTABLISH_SOFTINTR) == 0) {
257: bus_space_handle_t *hp;
258: struct fhc_intr_reg *intrregs;
259:
260: hp = fhc_find_intr_handle(sc, ihandle);
261: if (hp == NULL) {
262: printf(": can't find intr handle\n");
263: return (NULL);
264: }
265:
266: intrregs = bus_space_vaddr(sc->sc_bt, *hp);
267: intrmapptr = &intrregs->imap;
268: intrclrptr = &intrregs->iclr;
269: vec = ((sc->sc_ign << INTMAP_IGN_SHIFT) & INTMAP_IGN) |
270: INTINO(ihandle);
271: } else
272: vec = INTVEC(ihandle);
273:
274: ih = bus_intr_allocate(t0, handler, arg, vec, level, intrmapptr,
275: intrclrptr, what);
276: if (ih == NULL)
277: return (NULL);
278:
279: intr_establish(ih->ih_pil, ih);
280:
281: if (intrmapptr != NULL) {
282: u_int64_t r;
283:
284: r = *intrmapptr;
285: r |= INTMAP_V;
286: *intrmapptr = r;
287: r = *intrmapptr;
288: ih->ih_number |= r & INTMAP_INR;
289: }
290:
291: return (ih);
292: }
293:
294: void
295: fhc_led_blink(void *vsc, int on)
296: {
297: struct fhc_softc *sc = vsc;
298: int s;
299: u_int32_t r;
300:
301: s = splhigh();
302: r = bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL);
303: if (on)
304: r |= FHC_P_CTRL_RLED;
305: else
306: r &= ~FHC_P_CTRL_RLED;
307: r &= ~(FHC_P_CTRL_AOFF | FHC_P_CTRL_BOFF | FHC_P_CTRL_SLINE);
308: bus_space_write_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL, r);
309: bus_space_read_4(sc->sc_bt, sc->sc_preg, FHC_P_CTRL);
310: splx(s);
311: }
CVSweb