1 /*        $NetBSD: ffb_mainbus.c,v 1.15 2018/09/03 16:29:27 riastradh Exp $     */
2 /*        $OpenBSD: creator_mainbus.c,v 1.4 2002/07/26 16:39:04 jason Exp $     */
3 
4 /*
5  * Copyright (c) 2002 Jason L. Wright (jason@thought.net),
6  *  Federico G. Schwindt (fgsch@openbsd.org)
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed by Jason L. Wright
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ffb_mainbus.c,v 1.15 2018/09/03 16:29:27 riastradh Exp $");
38 
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 
45 #include <sys/bus.h>
46 #include <machine/autoconf.h>
47 #include <machine/openfirm.h>
48 
49 #include <dev/wscons/wsconsio.h>
50 #include <dev/sun/fbio.h>
51 #include <dev/sun/fbvar.h>
52 
53 #include <sparc64/dev/ffbreg.h>
54 #include <sparc64/dev/ffbvar.h>
55 
56 extern int prom_stdout_node;
57 
58 int       ffb_mainbus_match(device_t, cfdata_t, void *);
59 void      ffb_mainbus_attach(device_t, device_t, void *);
60 
61 CFATTACH_DECL_NEW(ffb_mainbus, sizeof(struct ffb_softc),
62     ffb_mainbus_match, ffb_mainbus_attach, NULL, NULL);
63 
64 int
ffb_mainbus_match(device_t parent,cfdata_t match,void * aux)65 ffb_mainbus_match(device_t parent, cfdata_t match, void *aux)
66 {
67           struct mainbus_attach_args *ma = aux;
68 
69           if (strcmp(ma->ma_name, "SUNW,ffb") == 0 ||
70               strcmp(ma->ma_name, "SUNW,afb") == 0)
71                     return 1;
72           return 0;
73 }
74 
75 void
ffb_mainbus_attach(device_t parent,device_t self,void * aux)76 ffb_mainbus_attach(device_t parent, device_t self, void *aux)
77 {
78           struct ffb_softc *sc = device_private(self);
79           struct mainbus_attach_args *ma = aux;
80           int i, nregs;
81 
82           sc->sc_dev = self;
83           sc->sc_bt = ma->ma_bustag;
84 
85           nregs = uimin(ma->ma_nreg, FFB_NREGS);
86 
87           if (nregs < FFB_REG_DFB24) {
88                     printf(": no dfb24 regs found\n");
89                     goto fail1;
90           }
91 
92 
93           if (bus_space_map(sc->sc_bt, ma->ma_reg[FFB_REG_FBC].ur_paddr,
94               ma->ma_reg[FFB_REG_FBC].ur_len, 0, &sc->sc_fbc_h)) {
95                     printf(": failed to map fbc\n");
96                     goto fail1;
97           }
98 
99           if (bus_space_map(sc->sc_bt, ma->ma_reg[FFB_REG_DAC].ur_paddr,
100               ma->ma_reg[FFB_REG_DAC].ur_len, 0, &sc->sc_dac_h)) {
101                     printf(": failed to map dac\n");
102                     goto unmap_fbc;
103           }
104 
105           if (bus_space_map(sc->sc_bt, ma->ma_reg[FFB_REG_SFB32].ur_paddr,
106               ma->ma_reg[FFB_REG_SFB32].ur_len,
107               BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE,
108               &sc->sc_sfb32_h)) {
109                     panic(": failed to map SFB32\n");
110                     goto unmap_dac;
111           }
112           sc->sc_sfb32 = bus_space_vaddr(sc->sc_bt, sc->sc_sfb32_h);
113 
114           for (i = 0; i < nregs; i++) {
115                     sc->sc_addrs[i] = ma->ma_reg[i].ur_paddr;
116                     sc->sc_sizes[i] = ma->ma_reg[i].ur_len;
117           }
118           sc->sc_nreg = nregs;
119 
120           sc->sc_console = (prom_stdout_node == ma->ma_node);
121           sc->sc_node = ma->ma_node;
122 
123           if (strcmp(ma->ma_name, "SUNW,afb") == 0)
124                     sc->sc_type = FFB_AFB;
125 
126           ffb_attach(self);
127 
128           return;
129 
130 unmap_dac:
131           bus_space_unmap(sc->sc_bt, sc->sc_dac_h,
132                         ma->ma_reg[FFB_REG_DAC].ur_len);
133 
134 unmap_fbc:
135           bus_space_unmap(sc->sc_bt, sc->sc_fbc_h,
136                         ma->ma_reg[FFB_REG_FBC].ur_len);
137 fail1:
138           return;
139 }
140