sti_sgc.c revision 1.4 1 /* $NetBSD: sti_sgc.c,v 1.4 2020/12/23 08:36:47 tsutsui Exp $ */
2 /* $OpenBSD: sti_sgc.c,v 1.14 2007/05/26 00:36:03 krw Exp $ */
3
4 /*
5 * Copyright (c) 2005, 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_sgc.c,v 1.4 2020/12/23 08:36:47 tsutsui Exp $");
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/bus.h>
35
36 #include <uvm/uvm_extern.h>
37
38 #include <dev/wscons/wsconsio.h>
39 #include <dev/wscons/wsdisplayvar.h>
40
41 #include <dev/ic/stireg.h>
42 #include <dev/ic/stivar.h>
43
44 #include <hp300/dev/sgcvar.h>
45 #include <hp300/dev/sti_sgcvar.h>
46 #include <machine/autoconf.h>
47
48 struct sti_sgc_softc {
49 struct sti_softc sc_sti;
50
51 paddr_t sc_bitmap;
52 };
53
54 /*
55 * 425e EVRX specific hardware
56 */
57 /*
58 * EVRX RAMDAC (Bt458) is found at offset 0x060000 from SGC bus PA and
59 * offset 0x040000 length 0x1c0000 is mapped in MI sti via ROM region 2
60 */
61 #define STI_EVRX_REGNO2OFFSET 0x020000
62 #define STI_EVRX_FBOFFSET 0x200000
63
64 #define EVRX_BT458_ADDR (STI_EVRX_REGNO2OFFSET + 0x200 + 2)
65 #define EVRX_BT458_CMAP (STI_EVRX_REGNO2OFFSET + 0x204 + 2)
66 #define EVRX_BT458_CTRL (STI_EVRX_REGNO2OFFSET + 0x208 + 2)
67 #define EVRX_BT458_OMAP (STI_EVRX_REGNO2OFFSET + 0x20C + 2)
68
69 /* from HP-UX /usr/lib/libddevrx.a */
70 #define EVRX_MAGIC00 (STI_EVRX_REGNO2OFFSET + 0x600)
71 #define EVRX_MAGIC04 (STI_EVRX_REGNO2OFFSET + 0x604)
72 #define EVRX_MAGIC08 (STI_EVRX_REGNO2OFFSET + 0x608)
73 #define EVRX_MAGIC0C (STI_EVRX_REGNO2OFFSET + 0x60c)
74 #define EVRX_MAGIC10 (STI_EVRX_REGNO2OFFSET + 0x610)
75 #define EVRX_MAGIC10_BSY 0x00010000
76 #define EVRX_MAGIC18 (STI_EVRX_REGNO2OFFSET + 0x618)
77 #define EVRX_MAGIC1C (STI_EVRX_REGNO2OFFSET + 0x61c)
78
79 /*
80 * HP A1659A CRX specific hardware
81 */
82 #define STI_CRX_FBOFFSET 0x01000000
83
84 static int sticonslot = -1;
85 static struct bus_space_tag sticn_tag;
86 static struct sti_rom sticn_rom;
87 static struct sti_screen sticn_scr;
88 static bus_addr_t sticn_bases[STI_REGION_MAX];
89
90 static int sti_sgc_match(device_t, struct cfdata *, void *);
91 static void sti_sgc_attach(device_t, device_t, void *);
92
93 static int sti_sgc_probe(bus_space_tag_t, int);
94
95 CFATTACH_DECL_NEW(sti_sgc, sizeof(struct sti_sgc_softc),
96 sti_sgc_match, sti_sgc_attach, NULL, NULL);
97
98 /* 425e EVRX/CRX specific access functions */
99 static int sti_evrx_putcmap(struct sti_screen *, u_int, u_int);
100 static void sti_evrx_resetramdac(struct sti_screen *);
101 static void sti_evrx_resetcmap(struct sti_screen *);
102 static void sti_evrx_setupfb(struct sti_screen *);
103 static paddr_t sti_m68k_mmap(void *, void *, off_t, int);
104
105 static const struct wsdisplay_accessops sti_m68k_accessops = {
106 sti_ioctl,
107 sti_m68k_mmap,
108 sti_alloc_screen,
109 sti_free_screen,
110 sti_show_screen,
111 sti_load_font
112 };
113
114 static int
115 sti_sgc_match(device_t parent, struct cfdata *cf, void *aux)
116 {
117 struct sgc_attach_args *saa = aux;
118
119 /*
120 * If we already probed it successfully as a console device, go ahead,
121 * since we will not be able to bus_space_map() again.
122 */
123 if (saa->saa_slot == sticonslot)
124 return 1;
125
126 return sti_sgc_probe(saa->saa_iot, saa->saa_slot);
127 }
128
129 static void
130 sti_sgc_attach(device_t parent, device_t self, void *aux)
131 {
132 struct sti_sgc_softc *sc = device_private(self);
133 struct sti_softc *ssc = &sc->sc_sti;
134 struct sgc_attach_args *saa = aux;
135 struct sti_screen *scr;
136 bus_space_tag_t bst;
137 bus_space_handle_t romh;
138 bus_addr_t base;
139 struct wsemuldisplaydev_attach_args waa;
140 u_int romend;
141 struct sti_dd *rom_dd;
142 uint32_t grid0;
143 int i;
144
145 ssc->sc_dev = self;
146 bst = saa->saa_iot;
147 base = (bus_addr_t)sgc_slottopa(saa->saa_slot);
148
149 if (saa->saa_slot == sticonslot) {
150 ssc->sc_flags |= STI_CONSOLE | STI_ATTACHED;
151 ssc->sc_rom = &sticn_rom;
152 ssc->sc_rom->rom_softc = ssc;
153 ssc->sc_scr = &sticn_scr;
154 ssc->sc_scr->scr_rom = ssc->sc_rom;
155 memcpy(ssc->bases, sticn_bases, sizeof(ssc->bases));
156
157 sti_describe(ssc);
158 } else {
159 if (bus_space_map(bst, base, PAGE_SIZE, 0, &romh)) {
160 aprint_error(": can't map ROM");
161 return;
162 }
163 /*
164 * Compute real PROM size
165 */
166 romend = sti_rom_size(bst, romh);
167
168 bus_space_unmap(bst, romh, PAGE_SIZE);
169
170 if (bus_space_map(bst, base, romend, 0, &romh)) {
171 aprint_error(": can't map frame buffer");
172 return;
173 }
174
175 ssc->bases[0] = romh;
176 for (i = 0; i < STI_REGION_MAX; i++)
177 ssc->bases[i] = base;
178
179 if (sti_attach_common(ssc, bst, bst, romh,
180 STI_CODEBASE_ALT) != 0)
181 return;
182 }
183
184 /* Identify the board model by dd_grid */
185 rom_dd = &ssc->sc_rom->rom_dd;
186 grid0 = rom_dd->dd_grid[0];
187 scr = ssc->sc_scr;
188
189 switch (grid0) {
190 case STI_DD_EVRX:
191 /*
192 * 425e on-board EVRX framebuffer.
193 * bitmap memory can be accessed at offset +0x200000.
194 */
195 sc->sc_bitmap = base + STI_EVRX_FBOFFSET;
196
197 aprint_normal_dev(self, "Enable mmap support\n");
198
199 /*
200 * initialize Bt458 RAMDAC and preserve initial color map
201 */
202 sti_evrx_resetramdac(scr);
203 sti_evrx_resetcmap(scr);
204
205 scr->setupfb = sti_evrx_setupfb;
206 scr->putcmap = sti_evrx_putcmap;
207
208 scr->scr_wsmode = WSDISPLAYIO_MODE_EMUL;
209 waa.console = ssc->sc_flags & STI_CONSOLE ? 1 : 0;
210 waa.scrdata = &scr->scr_screenlist;
211 waa.accessops = &sti_m68k_accessops;
212 waa.accesscookie = scr;
213
214 config_found(ssc->sc_dev, &waa, wsemuldisplaydevprint);
215 break;
216
217 case STI_DD_CRX:
218 /*
219 * HP A1659A CRX on some 425t variants.
220 * bitmap memory can be accessed at offset +0x1000000.
221 */
222 sc->sc_bitmap = base + STI_CRX_FBOFFSET;
223
224 aprint_normal_dev(self, "Enable mmap support\n");
225
226 scr->scr_wsmode = WSDISPLAYIO_MODE_EMUL;
227 waa.console = ssc->sc_flags & STI_CONSOLE ? 1 : 0;
228 waa.scrdata = &scr->scr_screenlist;
229 waa.accessops = &sti_m68k_accessops;
230 waa.accesscookie = scr;
231
232 config_found(ssc->sc_dev, &waa, wsemuldisplaydevprint);
233 break;
234 default:
235 /*
236 * Unsupported variants.
237 * Use default common sti(4) attachment (no bitmap support).
238 */
239 sti_end_attach(ssc);
240 break;
241 }
242 }
243
244 static int
245 sti_sgc_probe(bus_space_tag_t iot, int slot)
246 {
247 bus_space_handle_t ioh;
248 int devtype;
249
250 if (bus_space_map(iot, (bus_addr_t)sgc_slottopa(slot),
251 PAGE_SIZE, 0, &ioh))
252 return 0;
253
254 devtype = bus_space_read_1(iot, ioh, 3);
255
256 bus_space_unmap(iot, ioh, PAGE_SIZE);
257
258 /*
259 * This might not be reliable enough. On the other hand, non-STI
260 * SGC cards will apparently not initialize in an hp300, to the
261 * point of not even answering bus probes (checked with an
262 * Harmony/FDDI SGC card).
263 */
264 if (devtype != STI_DEVTYPE1 && devtype != STI_DEVTYPE4)
265 return 0;
266
267 return 1;
268 }
269
270 static int
271 sti_evrx_putcmap(struct sti_screen *scr, u_int index, u_int count)
272 {
273 struct sti_rom *rom = scr->scr_rom;
274 bus_space_tag_t bst = rom->memt;
275 bus_space_handle_t bsh = rom->regh[2];
276 int i;
277
278 /* magic setup from HP-UX */
279 bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
280 bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
281 for (i = index; i < index + count; i++) {
282 /* this is what HP-UX woodDownloadCmap() does */
283 while ((bus_space_read_4(bst, bsh, EVRX_MAGIC10) &
284 EVRX_MAGIC10_BSY) != 0)
285 continue;
286 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, i);
287 bus_space_write_1(bst, bsh, EVRX_BT458_CMAP, scr->scr_rcmap[i]);
288 bus_space_write_1(bst, bsh, EVRX_BT458_CMAP, scr->scr_gcmap[i]);
289 bus_space_write_4(bst, bsh, EVRX_MAGIC10, scr->scr_bcmap[i]);
290 }
291 return 0;
292 }
293
294 static void
295 sti_evrx_resetramdac(struct sti_screen *scr)
296 {
297 struct sti_rom *rom = scr->scr_rom;
298 bus_space_tag_t bst = rom->memt;
299 bus_space_handle_t bsh = rom->regh[2];
300 #if 0
301 int i;
302 #endif
303
304 /*
305 * Initialize the Bt458. When we write to control registers,
306 * the address is not incremented automatically. So we specify
307 * it ourselves for each control register.
308 */
309
310 /* all planes will be read */
311 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x04);
312 bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0xff);
313
314 /* all planes have non-blink */
315 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x05);
316 bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x00);
317
318 /* pallete enabled, ovly plane disabled */
319 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x06);
320 bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x40);
321
322 /* no test mode */
323 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x07);
324 bus_space_write_1(bst, bsh, EVRX_BT458_CTRL, 0x00);
325
326 /* magic initialization from HP-UX woodInitializeHardware() */
327 bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
328 bus_space_write_4(bst, bsh, EVRX_MAGIC04, 0x00000001);
329 bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
330 bus_space_write_4(bst, bsh, EVRX_MAGIC0C, 0x00000001);
331 bus_space_write_4(bst, bsh, EVRX_MAGIC18, 0xFFFFFFFF);
332 bus_space_write_4(bst, bsh, EVRX_MAGIC1C, 0x00000000);
333
334 #if 0
335 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, 0x00);
336 for (i = 0; i < 4; i++) {
337 bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
338 bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
339 bus_space_write_1(bst, bsh, EVRX_BT458_OMAP, 0x00);
340 }
341 #endif
342 }
343
344 static void
345 sti_evrx_resetcmap(struct sti_screen *scr)
346 {
347 struct sti_rom *rom = scr->scr_rom;
348 bus_space_tag_t bst = rom->memt;
349 bus_space_handle_t bsh = rom->regh[2];
350 int i;
351
352 /* magic setup from HP-UX */
353 bus_space_write_4(bst, bsh, EVRX_MAGIC08, 0x00000001);
354 bus_space_write_4(bst, bsh, EVRX_MAGIC00, 0x00000001);
355
356 /* preserve palette values initialized by STI firmware */
357 for (i = 0; i < STI_NCMAP; i++) {
358 /* this is what HP-UX woodUploadCmap() does */
359 while ((bus_space_read_4(bst, bsh, EVRX_MAGIC10) &
360 EVRX_MAGIC10_BSY) != 0)
361 continue;
362 bus_space_write_1(bst, bsh, EVRX_BT458_ADDR, i);
363 scr->scr_rcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT458_CMAP);
364 scr->scr_gcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT458_CMAP);
365 scr->scr_bcmap[i] = bus_space_read_1(bst, bsh, EVRX_BT458_CMAP);
366 }
367 }
368
369 static void
370 sti_evrx_setupfb(struct sti_screen *scr)
371 {
372
373 sti_init(scr, 0);
374 sti_evrx_resetramdac(scr);
375 }
376
377 static paddr_t
378 sti_m68k_mmap(void *v, void *vs, off_t offset, int prot)
379 {
380 struct sti_screen *scr = (struct sti_screen *)v;
381 struct sti_rom *rom = scr->scr_rom;
382 struct sti_softc *ssc = rom->rom_softc;
383 struct sti_sgc_softc *sc = device_private(ssc->sc_dev);
384 paddr_t cookie = -1;
385
386 if ((offset & PAGE_MASK) != 0)
387 return -1;
388
389 switch (scr->scr_wsmode) {
390 case WSDISPLAYIO_MODE_MAPPED:
391 /* not implemented yet; what should be shown? */
392 break;
393 case WSDISPLAYIO_MODE_DUMBFB:
394 if (offset >= 0 && offset < (scr->fbwidth * scr->fbheight))
395 cookie = m68k_btop(sc->sc_bitmap + offset);
396 break;
397 default:
398 break;
399 }
400
401 return cookie;
402 }
403
404 int
405 sti_sgc_cnprobe(bus_space_tag_t bst, bus_addr_t addr, int slot)
406 {
407 void *va;
408 bus_space_handle_t romh;
409 int devtype, rv = 0;
410
411 if (bus_space_map(bst, addr, PAGE_SIZE, 0, &romh))
412 return 0;
413
414 va = bus_space_vaddr(bst, romh);
415 if (badaddr(va))
416 goto out;
417
418 devtype = bus_space_read_1(bst, romh, 3);
419 if (devtype == STI_DEVTYPE1 || devtype == STI_DEVTYPE4)
420 rv = 1;
421
422 out:
423 bus_space_unmap(bst, romh, PAGE_SIZE);
424 return rv;
425 }
426
427 void
428 sti_sgc_cnattach(bus_space_tag_t bst, bus_addr_t addr, int slot)
429 {
430 int i;
431
432 sticn_tag = *bst;
433
434 /* sticn_bases[0] will be fixed in sti_cnattach() */
435 for (i = 0; i < STI_REGION_MAX; i++)
436 sticn_bases[i] = addr;
437
438 sti_cnattach(&sticn_rom, &sticn_scr, &sticn_tag, sticn_bases,
439 STI_CODEBASE_ALT);
440
441 sticonslot = slot;
442 }
443