sti_dio.c revision 1.4 1 /* $NetBSD: sti_dio.c,v 1.4 2025/05/28 17:33:38 tsutsui Exp $ */
2 /* $OpenBSD: sti_dio.c,v 1.1 2011/08/18 20:02:57 miod Exp $ */
3
4 /*
5 * Copyright (c) 2005, 2011, Miodrag Vallat
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/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: sti_dio.c,v 1.4 2025/05/28 17:33:38 tsutsui Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/bus.h>
35
36 #include <machine/autoconf.h>
37
38 #include <hp300/dev/dioreg.h>
39 #include <hp300/dev/diovar.h>
40 #include <hp300/dev/diodevs.h>
41 #include <hp300/dev/sti_diovar.h>
42 #include <hp300/dev/sti_machdep.h>
43
44 static int sti_dio_match(device_t, cfdata_t, void *);
45 static void sti_dio_attach(device_t, device_t, void *);
46
47 static int sti_dio_probe(bus_space_tag_t, int);
48
49 CFATTACH_DECL_NEW(sti_dio, sizeof(struct sti_machdep_softc),
50 sti_dio_match, sti_dio_attach, NULL, NULL);
51
52 static int
53 sti_dio_match(device_t parent, cfdata_t cf, void *aux)
54 {
55 struct dio_attach_args *da = aux;
56
57 if (da->da_id != DIO_DEVICE_ID_FRAMEBUFFER ||
58 (da->da_secid != DIO_DEVICE_SECID_A1474MID &&
59 da->da_secid != DIO_DEVICE_SECID_A147xVGA))
60 return 0;
61
62 /*
63 * If we already probed it successfully as a console device, go ahead,
64 * since we will not be able to bus_space_map() again.
65 */
66 if (da->da_scode == conscode ||
67 sti_dio_probe(da->da_bst, da->da_scode) != 0)
68 return 10; /* Beat old gendiofb(4) */
69
70 return 0;
71 }
72
73 static void
74 sti_dio_attach(device_t parent, device_t self, void *aux)
75 {
76 struct sti_machdep_softc *sc = device_private(self);
77 struct sti_softc *ssc = &sc->sc_sti;
78 struct dio_attach_args *da = aux;
79 bus_addr_t base;
80 bus_space_tag_t bst;
81 bus_space_handle_t romh;
82 u_int romend;
83 int i;
84
85 ssc->sc_dev = self;
86 bst = da->da_bst;
87
88 base = (paddr_t)dio_scodetopa(da->da_scode + STI_DIO_SCODE_OFFSET);
89 sc->sc_base = base;
90
91 /*
92 * If we already probed it successfully as a console device, go ahead,
93 * since we will not be able to bus_space_map() again.
94 */
95 if (da->da_scode == conscode) {
96 sti_machdep_attach_console(sc);
97 } else {
98 if (bus_space_map(bst, base, PAGE_SIZE, 0, &romh)) {
99 aprint_error(": can't map frame buffer");
100 return;
101 }
102
103 /*
104 * Compute real PROM size
105 */
106 romend = sti_rom_size(bst, romh);
107
108 bus_space_unmap(bst, romh, PAGE_SIZE);
109
110 if (bus_space_map(bst, base, romend, 0, &romh)) {
111 aprint_error(": can't map frame buffer");
112 return;
113 }
114
115 ssc->bases[0] = romh;
116 for (i = 1; i < STI_REGION_MAX; i++)
117 ssc->bases[i] = base;
118
119 if (sti_attach_common(ssc, bst, bst, romh,
120 STI_CODEBASE_M68K) != 0)
121 return;
122 }
123
124 sti_machdep_attach(sc);
125 }
126
127 static int
128 sti_dio_probe(bus_space_tag_t bst, int scode)
129 {
130 bus_space_handle_t bsh;
131 bus_addr_t addr, base;
132 int devtype;
133 u_int span;
134
135 /*
136 * Sanity checks:
137 * these devices provide both a DIO and an STI ROM. We expect the
138 * DIO ROM to be a DIO-II ROM (i.e. to be at a DIO-II select code)
139 * and report the device as spanning at least four select codes.
140 */
141
142 if (!DIO_ISDIOII(scode))
143 return 0;
144
145 addr = (bus_addr_t)dio_scodetopa(scode);
146 if (bus_space_map(bst, addr, PAGE_SIZE, 0, &bsh))
147 return 0;
148 span = bus_space_read_1(bst, bsh, DIOII_SIZEOFF);
149 bus_space_unmap(bst, bsh, PAGE_SIZE);
150
151 if (span < STI_DIO_SIZE - 1)
152 return 0;
153
154 base = (bus_addr_t)dio_scodetopa(scode + STI_DIO_SCODE_OFFSET);
155 if (bus_space_map(bst, base, PAGE_SIZE, 0, &bsh))
156 return 0;
157 devtype = bus_space_read_1(bst, bsh, 3);
158 bus_space_unmap(bst, bsh, PAGE_SIZE);
159
160 if (devtype != STI_DEVTYPE1 && devtype != STI_DEVTYPE4)
161 return 0;
162
163 return 1;
164 }
165
166 int
167 sti_dio_cnprobe(bus_space_tag_t bst, bus_addr_t addr, int scode)
168 {
169
170 if (sti_dio_probe(bst, scode) == 0) {
171 /* not found */
172 return 1;
173 }
174 conscode = scode;
175
176 return 0;
177 }
178
179 void
180 sti_dio_cnattach(bus_space_tag_t bst, int scode)
181 {
182 paddr_t base;
183
184 base = (paddr_t)dio_scodetopa(scode + STI_DIO_SCODE_OFFSET);
185 sti_machdep_cnattach(bst, base);
186 }
187