sti_dio.c revision 1.1 1 1.1 tsutsui /* $NetBSD: sti_dio.c,v 1.1 2025/05/01 06:11:21 tsutsui Exp $ */
2 1.1 tsutsui /* $OpenBSD: sti_dio.c,v 1.1 2011/08/18 20:02:57 miod Exp $ */
3 1.1 tsutsui
4 1.1 tsutsui /*
5 1.1 tsutsui * Copyright (c) 2005, 2011, Miodrag Vallat
6 1.1 tsutsui *
7 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
8 1.1 tsutsui * modification, are permitted provided that the following conditions
9 1.1 tsutsui * are met:
10 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
11 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
12 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
14 1.1 tsutsui * documentation and/or other materials provided with the distribution.
15 1.1 tsutsui *
16 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 tsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.1 tsutsui * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 1.1 tsutsui * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 1.1 tsutsui * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1 tsutsui * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1 tsutsui * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 tsutsui * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 1.1 tsutsui * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 1.1 tsutsui * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE.
27 1.1 tsutsui *
28 1.1 tsutsui */
29 1.1 tsutsui #include <sys/cdefs.h>
30 1.1 tsutsui __KERNEL_RCSID(0, "$NetBSD: sti_dio.c,v 1.1 2025/05/01 06:11:21 tsutsui Exp $");
31 1.1 tsutsui
32 1.1 tsutsui #include <sys/param.h>
33 1.1 tsutsui #include <sys/device.h>
34 1.1 tsutsui #include <sys/bus.h>
35 1.1 tsutsui
36 1.1 tsutsui #include <machine/autoconf.h>
37 1.1 tsutsui
38 1.1 tsutsui #include <hp300/dev/dioreg.h>
39 1.1 tsutsui #include <hp300/dev/diovar.h>
40 1.1 tsutsui #include <hp300/dev/diodevs.h>
41 1.1 tsutsui #include <hp300/dev/sti_diovar.h>
42 1.1 tsutsui #include <hp300/dev/sti_machdep.h>
43 1.1 tsutsui
44 1.1 tsutsui /* DIO attachment defines */
45 1.1 tsutsui #define STI_DIO_SCODE_OFFSET 0x02 /* offset to SGC rom, in select codes */
46 1.1 tsutsui #define STI_DIO_SIZE 0x10 /* expected total device size
47 1.1 tsutsui in DIO-II size units */
48 1.1 tsutsui
49 1.1 tsutsui static int sti_dio_match(device_t, cfdata_t, void *);
50 1.1 tsutsui static void sti_dio_attach(device_t, device_t, void *);
51 1.1 tsutsui
52 1.1 tsutsui static int sti_dio_probe(bus_space_tag_t, int);
53 1.1 tsutsui
54 1.1 tsutsui CFATTACH_DECL_NEW(sti_dio, sizeof(struct sti_machdep_softc),
55 1.1 tsutsui sti_dio_match, sti_dio_attach, NULL, NULL);
56 1.1 tsutsui
57 1.1 tsutsui static int
58 1.1 tsutsui sti_dio_match(device_t parent, cfdata_t cf, void *aux)
59 1.1 tsutsui {
60 1.1 tsutsui struct dio_attach_args *da = aux;
61 1.1 tsutsui
62 1.1 tsutsui if (da->da_id != DIO_DEVICE_ID_FRAMEBUFFER ||
63 1.1 tsutsui (da->da_secid != DIO_DEVICE_SECID_A1474MID &&
64 1.1 tsutsui da->da_secid != DIO_DEVICE_SECID_A147xVGA))
65 1.1 tsutsui return 0;
66 1.1 tsutsui
67 1.1 tsutsui /*
68 1.1 tsutsui * If we already probed it successfully as a console device, go ahead,
69 1.1 tsutsui * since we will not be able to bus_space_map() again.
70 1.1 tsutsui */
71 1.1 tsutsui if (da->da_scode == conscode ||
72 1.1 tsutsui sti_dio_probe(da->da_bst, da->da_scode) != 0)
73 1.1 tsutsui return 10; /* Beat old gendiofb(4) */
74 1.1 tsutsui
75 1.1 tsutsui return 0;
76 1.1 tsutsui }
77 1.1 tsutsui
78 1.1 tsutsui static void
79 1.1 tsutsui sti_dio_attach(device_t parent, device_t self, void *aux)
80 1.1 tsutsui {
81 1.1 tsutsui struct sti_machdep_softc *sc = device_private(self);
82 1.1 tsutsui struct sti_softc *ssc = &sc->sc_sti;
83 1.1 tsutsui struct dio_attach_args *da = aux;
84 1.1 tsutsui bus_addr_t base;
85 1.1 tsutsui bus_space_tag_t bst;
86 1.1 tsutsui bus_space_handle_t romh;
87 1.1 tsutsui u_int romend;
88 1.1 tsutsui int i;
89 1.1 tsutsui
90 1.1 tsutsui ssc->sc_dev = self;
91 1.1 tsutsui bst = da->da_bst;
92 1.1 tsutsui
93 1.1 tsutsui base = (paddr_t)dio_scodetopa(da->da_scode + STI_DIO_SCODE_OFFSET);
94 1.1 tsutsui sc->sc_base = base;
95 1.1 tsutsui
96 1.1 tsutsui /*
97 1.1 tsutsui * If we already probed it successfully as a console device, go ahead,
98 1.1 tsutsui * since we will not be able to bus_space_map() again.
99 1.1 tsutsui */
100 1.1 tsutsui if (da->da_scode == conscode) {
101 1.1 tsutsui sti_machdep_attach_console(sc);
102 1.1 tsutsui } else {
103 1.1 tsutsui if (bus_space_map(bst, base, PAGE_SIZE, 0, &romh)) {
104 1.1 tsutsui aprint_error(": can't map frame buffer");
105 1.1 tsutsui return;
106 1.1 tsutsui }
107 1.1 tsutsui
108 1.1 tsutsui /*
109 1.1 tsutsui * Compute real PROM size
110 1.1 tsutsui */
111 1.1 tsutsui romend = sti_rom_size(bst, romh);
112 1.1 tsutsui
113 1.1 tsutsui bus_space_unmap(bst, romh, PAGE_SIZE);
114 1.1 tsutsui
115 1.1 tsutsui if (bus_space_map(bst, base, romend, 0, &romh)) {
116 1.1 tsutsui aprint_error(": can't map frame buffer");
117 1.1 tsutsui return;
118 1.1 tsutsui }
119 1.1 tsutsui
120 1.1 tsutsui ssc->bases[0] = romh;
121 1.1 tsutsui for (i = 1; i < STI_REGION_MAX; i++)
122 1.1 tsutsui ssc->bases[i] = base;
123 1.1 tsutsui
124 1.1 tsutsui if (sti_attach_common(ssc, bst, bst, romh,
125 1.1 tsutsui STI_CODEBASE_ALT) != 0)
126 1.1 tsutsui return;
127 1.1 tsutsui }
128 1.1 tsutsui
129 1.1 tsutsui sti_machdep_attach(sc);
130 1.1 tsutsui }
131 1.1 tsutsui
132 1.1 tsutsui static int
133 1.1 tsutsui sti_dio_probe(bus_space_tag_t bst, int scode)
134 1.1 tsutsui {
135 1.1 tsutsui bus_space_handle_t bsh;
136 1.1 tsutsui bus_addr_t addr, base;
137 1.1 tsutsui int devtype;
138 1.1 tsutsui u_int span;
139 1.1 tsutsui
140 1.1 tsutsui /*
141 1.1 tsutsui * Sanity checks:
142 1.1 tsutsui * these devices provide both a DIO and an STI ROM. We expect the
143 1.1 tsutsui * DIO ROM to be a DIO-II ROM (i.e. to be at a DIO-II select code)
144 1.1 tsutsui * and report the device as spanning at least four select codes.
145 1.1 tsutsui */
146 1.1 tsutsui
147 1.1 tsutsui if (!DIO_ISDIOII(scode))
148 1.1 tsutsui return 0;
149 1.1 tsutsui
150 1.1 tsutsui addr = (bus_addr_t)(bus_addr_t)dio_scodetopa(scode);
151 1.1 tsutsui if (bus_space_map(bst, addr, PAGE_SIZE, 0, &bsh))
152 1.1 tsutsui return 0;
153 1.1 tsutsui span = bus_space_read_1(bst, bsh, DIOII_SIZEOFF);
154 1.1 tsutsui bus_space_unmap(bst, bsh, PAGE_SIZE);
155 1.1 tsutsui
156 1.1 tsutsui if (span < STI_DIO_SIZE - 1)
157 1.1 tsutsui return 0;
158 1.1 tsutsui
159 1.1 tsutsui base = (bus_addr_t)dio_scodetopa(scode + STI_DIO_SCODE_OFFSET);
160 1.1 tsutsui if (bus_space_map(bst, base, PAGE_SIZE, 0, &bsh))
161 1.1 tsutsui return 0;
162 1.1 tsutsui devtype = bus_space_read_1(bst, bsh, 3);
163 1.1 tsutsui bus_space_unmap(bst, bsh, PAGE_SIZE);
164 1.1 tsutsui
165 1.1 tsutsui if (devtype != STI_DEVTYPE1 && devtype != STI_DEVTYPE4)
166 1.1 tsutsui return 0;
167 1.1 tsutsui
168 1.1 tsutsui return 1;
169 1.1 tsutsui }
170 1.1 tsutsui
171 1.1 tsutsui int
172 1.1 tsutsui sti_dio_cnprobe(bus_space_tag_t bst, bus_addr_t addr, int scode)
173 1.1 tsutsui {
174 1.1 tsutsui
175 1.1 tsutsui if (sti_dio_probe(bst, scode) == 0) {
176 1.1 tsutsui /* not found */
177 1.1 tsutsui return 1;
178 1.1 tsutsui }
179 1.1 tsutsui conscode = scode;
180 1.1 tsutsui
181 1.1 tsutsui return 0;
182 1.1 tsutsui }
183 1.1 tsutsui
184 1.1 tsutsui void
185 1.1 tsutsui sti_dio_cnattach(bus_space_tag_t bst, int scode)
186 1.1 tsutsui {
187 1.1 tsutsui paddr_t base;
188 1.1 tsutsui
189 1.1 tsutsui base = (paddr_t)dio_scodetopa(scode + STI_DIO_SCODE_OFFSET);
190 1.1 tsutsui sti_machdep_cnattach(bst, base);
191 1.1 tsutsui }
192