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