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