sisfb.c revision 1.5 1 1.1 bouyer /* $OpenBSD: sisfb.c,v 1.2 2010/12/26 15:40:59 miod Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 2010 Miodrag Vallat.
5 1.1 bouyer *
6 1.1 bouyer * Permission to use, copy, modify, and distribute this software for any
7 1.1 bouyer * purpose with or without fee is hereby granted, provided that the above
8 1.1 bouyer * copyright notice and this permission notice appear in all copies.
9 1.1 bouyer *
10 1.1 bouyer * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 bouyer * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 bouyer * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 bouyer * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 bouyer * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 bouyer * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 bouyer * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 bouyer */
18 1.1 bouyer
19 1.1 bouyer /*
20 1.1 bouyer * Minimalistic driver for the SIS315 Pro frame buffer found on the
21 1.1 bouyer * Lemote Fuloong 2F systems.
22 1.1 bouyer * Does not support accelaration, mode change, secondary output, or
23 1.1 bouyer * anything fancy.
24 1.1 bouyer */
25 1.1 bouyer
26 1.1 bouyer #include <sys/cdefs.h>
27 1.5 bouyer __KERNEL_RCSID(0, "$NetBSD: sisfb.c,v 1.5 2014/01/26 21:22:49 bouyer Exp $");
28 1.1 bouyer
29 1.1 bouyer #include <sys/param.h>
30 1.1 bouyer #include <sys/systm.h>
31 1.1 bouyer #include <sys/device.h>
32 1.1 bouyer #include <sys/bus.h>
33 1.5 bouyer #include <sys/kauth.h>
34 1.1 bouyer
35 1.1 bouyer #include <uvm/uvm_extern.h>
36 1.1 bouyer
37 1.1 bouyer #include <dev/pci/pcireg.h>
38 1.1 bouyer #include <dev/pci/pcivar.h>
39 1.1 bouyer #include <dev/pci/pcidevs.h>
40 1.1 bouyer #include <dev/pci/pciio.h>
41 1.1 bouyer
42 1.1 bouyer #include <dev/videomode/videomode.h>
43 1.1 bouyer
44 1.1 bouyer #include <dev/wscons/wsdisplayvar.h>
45 1.1 bouyer #include <dev/wscons/wsconsio.h>
46 1.1 bouyer #include <dev/wsfont/wsfont.h>
47 1.1 bouyer #include <dev/rasops/rasops.h>
48 1.1 bouyer #include <dev/wscons/wsdisplay_vconsvar.h>
49 1.1 bouyer #include <dev/pci/wsdisplay_pci.h>
50 1.1 bouyer
51 1.1 bouyer #include <dev/i2c/i2cvar.h>
52 1.1 bouyer
53 1.1 bouyer #include <dev/pci/sisfb.h>
54 1.1 bouyer
55 1.1 bouyer struct sisfb_softc;
56 1.1 bouyer
57 1.1 bouyer /* minimal frame buffer information, suitable for early console */
58 1.1 bouyer
59 1.1 bouyer struct sisfb {
60 1.1 bouyer struct sisfb_softc *sc;
61 1.1 bouyer uint8_t cmap[256 * 3];
62 1.1 bouyer
63 1.1 bouyer bus_space_tag_t fbt;
64 1.1 bouyer bus_space_handle_t fbh;
65 1.4 bouyer bus_addr_t fbbase;
66 1.4 bouyer bus_size_t fbsize;
67 1.1 bouyer
68 1.1 bouyer bus_space_tag_t mmiot;
69 1.1 bouyer bus_space_handle_t mmioh;
70 1.5 bouyer bus_addr_t mmiobase;
71 1.5 bouyer bus_size_t mmiosize;
72 1.1 bouyer
73 1.1 bouyer bus_space_tag_t iot;
74 1.1 bouyer bus_space_handle_t ioh;
75 1.5 bouyer bus_addr_t iobase;
76 1.5 bouyer bus_size_t iosize;
77 1.1 bouyer
78 1.1 bouyer struct vcons_screen vcs;
79 1.1 bouyer struct wsscreen_descr wsd;
80 1.1 bouyer
81 1.1 bouyer int fb_depth;
82 1.1 bouyer int fb_width;
83 1.1 bouyer int fb_height;
84 1.1 bouyer int fb_stride;
85 1.1 bouyer void *fb_addr;
86 1.1 bouyer };
87 1.1 bouyer
88 1.1 bouyer struct sisfb_softc {
89 1.1 bouyer device_t sc_dev;
90 1.1 bouyer struct sisfb *sc_fb;
91 1.1 bouyer struct sisfb sc_fb_store;
92 1.1 bouyer
93 1.1 bouyer struct vcons_data vd;
94 1.1 bouyer struct wsscreen_list sc_wsl;
95 1.1 bouyer const struct wsscreen_descr *sc_scrlist[1];
96 1.1 bouyer int sc_nscr;
97 1.1 bouyer int sc_mode;
98 1.4 bouyer
99 1.4 bouyer pci_chipset_tag_t sc_pc;
100 1.4 bouyer pcitag_t sc_pt;
101 1.1 bouyer };
102 1.1 bouyer
103 1.1 bouyer int sisfb_match(device_t, cfdata_t, void *);
104 1.1 bouyer void sisfb_attach(device_t, device_t, void *);
105 1.1 bouyer
106 1.1 bouyer CFATTACH_DECL_NEW(sisfb, sizeof(struct sisfb_softc),
107 1.1 bouyer sisfb_match, sisfb_attach, NULL, NULL);
108 1.1 bouyer
109 1.1 bouyer
110 1.1 bouyer int sisfb_alloc_screen(void *, const struct wsscreen_descr *, void **, int *,
111 1.1 bouyer int *, long *);
112 1.1 bouyer void sisfb_free_screen(void *, void *);
113 1.1 bouyer int sisfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
114 1.1 bouyer int sisfb_show_screen(void *, void *, int, void (*)(void *, int, int),
115 1.1 bouyer void *);
116 1.1 bouyer paddr_t sisfb_mmap(void *, void *, off_t, int);
117 1.1 bouyer void sisfb_init_screen(void *, struct vcons_screen *, int, long *);
118 1.1 bouyer
119 1.1 bouyer
120 1.1 bouyer struct wsdisplay_accessops sisfb_accessops = {
121 1.1 bouyer sisfb_ioctl,
122 1.1 bouyer sisfb_mmap,
123 1.1 bouyer sisfb_alloc_screen,
124 1.1 bouyer sisfb_free_screen,
125 1.1 bouyer sisfb_show_screen,
126 1.1 bouyer NULL, /* load_font */
127 1.1 bouyer NULL, /* poolc */
128 1.1 bouyer NULL /* scroll */
129 1.1 bouyer };
130 1.1 bouyer
131 1.1 bouyer int sisfb_getcmap(uint8_t *, struct wsdisplay_cmap *);
132 1.1 bouyer void sisfb_loadcmap(struct sisfb *, int, int);
133 1.1 bouyer int sisfb_putcmap(uint8_t *, struct wsdisplay_cmap *);
134 1.1 bouyer int sisfb_setup(struct sisfb *);
135 1.1 bouyer
136 1.1 bouyer static struct sisfb sisfbcn;
137 1.1 bouyer
138 1.1 bouyer /*
139 1.1 bouyer * Control Register access
140 1.1 bouyer *
141 1.1 bouyer * These are 8 bit registers; the choice of larger width types is intentional.
142 1.1 bouyer */
143 1.1 bouyer
144 1.1 bouyer #define SIS_VGA_PORT_OFFSET 0x380
145 1.1 bouyer
146 1.1 bouyer #define SEQ_ADDR (0x3c4 - SIS_VGA_PORT_OFFSET)
147 1.1 bouyer #define SEQ_DATA (0x3c5 - SIS_VGA_PORT_OFFSET)
148 1.1 bouyer #define DAC_ADDR (0x3c8 - SIS_VGA_PORT_OFFSET)
149 1.1 bouyer #define DAC_DATA (0x3c9 - SIS_VGA_PORT_OFFSET)
150 1.1 bouyer #undef CRTC_ADDR
151 1.1 bouyer #define CRTC_ADDR (0x3d4 - SIS_VGA_PORT_OFFSET)
152 1.1 bouyer #define CRTC_DATA (0x3d5 - SIS_VGA_PORT_OFFSET)
153 1.1 bouyer
154 1.1 bouyer #define CRTC_HDISPLE 0x01 /* horizontal display end */
155 1.1 bouyer #define CRTC_OVERFLL 0x07 /* overflow low */
156 1.1 bouyer #define CRTC_STARTADRH 0x0C /* linear start address mid */
157 1.1 bouyer #define CRTC_STARTADRL 0x0D /* linear start address low */
158 1.1 bouyer #define CRTC_VDE 0x12 /* vertical display end */
159 1.1 bouyer
160 1.1 bouyer
161 1.1 bouyer static inline uint sisfb_crtc_read(struct sisfb *, uint);
162 1.1 bouyer static inline void sisfb_crtc_write(struct sisfb *, uint, uint);
163 1.1 bouyer static inline uint sisfb_seq_read(struct sisfb *, uint);
164 1.1 bouyer static inline void sisfb_seq_write(struct sisfb *, uint, uint);
165 1.1 bouyer
166 1.1 bouyer static inline uint
167 1.1 bouyer sisfb_crtc_read(struct sisfb *fb, uint idx)
168 1.1 bouyer {
169 1.1 bouyer uint val;
170 1.1 bouyer bus_space_write_1(fb->iot, fb->ioh, CRTC_ADDR, idx);
171 1.1 bouyer val = bus_space_read_1(fb->iot, fb->ioh, CRTC_DATA);
172 1.1 bouyer #ifdef SIS_DEBUG
173 1.1 bouyer printf("CRTC %04x -> %02x\n", idx, val);
174 1.1 bouyer #endif
175 1.1 bouyer return val;
176 1.1 bouyer }
177 1.1 bouyer
178 1.1 bouyer static inline void
179 1.1 bouyer sisfb_crtc_write(struct sisfb *fb, uint idx, uint val)
180 1.1 bouyer {
181 1.1 bouyer #ifdef SIS_DEBUG
182 1.1 bouyer printf("CRTC %04x <- %02x\n", idx, val);
183 1.1 bouyer #endif
184 1.1 bouyer bus_space_write_1(fb->iot, fb->ioh, CRTC_ADDR, idx);
185 1.1 bouyer bus_space_write_1(fb->iot, fb->ioh, CRTC_DATA, val);
186 1.1 bouyer }
187 1.1 bouyer
188 1.1 bouyer static inline uint
189 1.1 bouyer sisfb_seq_read(struct sisfb *fb, uint idx)
190 1.1 bouyer {
191 1.1 bouyer uint val;
192 1.1 bouyer bus_space_write_1(fb->iot, fb->ioh, SEQ_ADDR, idx);
193 1.1 bouyer val = bus_space_read_1(fb->iot, fb->ioh, SEQ_DATA);
194 1.1 bouyer #ifdef SIS_DEBUG
195 1.1 bouyer printf("SEQ %04x -> %02x\n", idx, val);
196 1.1 bouyer #endif
197 1.1 bouyer return val;
198 1.1 bouyer }
199 1.1 bouyer
200 1.1 bouyer static inline void
201 1.1 bouyer sisfb_seq_write(struct sisfb *fb, uint idx, uint val)
202 1.1 bouyer {
203 1.1 bouyer #ifdef SIS_DEBUG
204 1.1 bouyer printf("SEQ %04x <- %02x\n", idx, val);
205 1.1 bouyer #endif
206 1.1 bouyer bus_space_write_1(fb->iot, fb->ioh, SEQ_ADDR, idx);
207 1.1 bouyer bus_space_write_1(fb->iot, fb->ioh, SEQ_DATA, val);
208 1.1 bouyer }
209 1.1 bouyer
210 1.1 bouyer int
211 1.1 bouyer sisfb_match(device_t parent, cfdata_t match, void *aux)
212 1.1 bouyer {
213 1.1 bouyer struct pci_attach_args *pa = (struct pci_attach_args *)aux;
214 1.1 bouyer
215 1.1 bouyer if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
216 1.1 bouyer return 0;
217 1.1 bouyer
218 1.1 bouyer if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_SIS)
219 1.1 bouyer return 0;
220 1.1 bouyer
221 1.1 bouyer if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SIS_315PRO_VGA)
222 1.1 bouyer return 100;
223 1.1 bouyer return (0);
224 1.1 bouyer }
225 1.1 bouyer
226 1.1 bouyer void
227 1.1 bouyer sisfb_attach(device_t parent, device_t self, void *aux)
228 1.1 bouyer {
229 1.1 bouyer struct sisfb_softc *sc = device_private(self);
230 1.1 bouyer struct pci_attach_args *pa = (struct pci_attach_args *)aux;
231 1.1 bouyer struct rasops_info *ri;
232 1.1 bouyer struct wsemuldisplaydev_attach_args waa;
233 1.1 bouyer struct sisfb *fb;
234 1.1 bouyer int console;
235 1.1 bouyer unsigned long defattr;
236 1.1 bouyer
237 1.1 bouyer sc->sc_dev = self;
238 1.1 bouyer console = sisfbcn.vcs.scr_ri.ri_hw != NULL;
239 1.1 bouyer
240 1.1 bouyer if (console)
241 1.1 bouyer fb = &sisfbcn;
242 1.1 bouyer else {
243 1.1 bouyer fb = &sc->sc_fb_store;
244 1.1 bouyer }
245 1.1 bouyer
246 1.1 bouyer sc->sc_fb = fb;
247 1.1 bouyer fb->sc = sc;
248 1.1 bouyer
249 1.2 drochner pci_aprint_devinfo(pa, NULL);
250 1.1 bouyer
251 1.4 bouyer sc->sc_pt = pa->pa_tag;
252 1.4 bouyer sc->sc_pc = pa->pa_pc;
253 1.4 bouyer
254 1.1 bouyer if (!console) {
255 1.1 bouyer fb->fbt = pa->pa_memt;
256 1.1 bouyer fb->mmiot = pa->pa_memt;
257 1.1 bouyer fb->iot = pa->pa_iot;
258 1.1 bouyer if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM,
259 1.4 bouyer BUS_SPACE_MAP_LINEAR, &fb->fbt, &fb->fbh,
260 1.4 bouyer &fb->fbbase, &fb->fbsize) != 0) {
261 1.1 bouyer aprint_error_dev(self, ": can't map frame buffer\n");
262 1.1 bouyer return;
263 1.1 bouyer }
264 1.1 bouyer
265 1.5 bouyer if (pci_mapreg_map(pa, PCI_MAPREG_START + 4,
266 1.5 bouyer PCI_MAPREG_TYPE_MEM, 0,
267 1.5 bouyer &fb->mmiot, &fb->mmioh, &fb->mmiobase, &fb->mmiosize) != 0) {
268 1.1 bouyer aprint_error_dev(self, ": can't map mmio area\n");
269 1.1 bouyer goto fail1;
270 1.1 bouyer }
271 1.1 bouyer
272 1.1 bouyer if (pci_mapreg_map(pa, PCI_MAPREG_START + 8, PCI_MAPREG_TYPE_IO,
273 1.5 bouyer 0, &fb->iot, &fb->ioh, &fb->iobase, &fb->iosize) != 0) {
274 1.1 bouyer aprint_error_dev(self, ": can't map registers\n");
275 1.1 bouyer goto fail2;
276 1.1 bouyer }
277 1.1 bouyer
278 1.1 bouyer
279 1.1 bouyer if (sisfb_setup(sc->sc_fb) != 0) {
280 1.1 bouyer aprint_error_dev(self, ": can't setup frame buffer\n");
281 1.1 bouyer goto fail3;
282 1.1 bouyer }
283 1.1 bouyer }
284 1.1 bouyer
285 1.1 bouyer aprint_normal_dev(self, ": %dx%dx%d frame buffer\n",
286 1.5 bouyer fb->vcs.scr_ri.ri_width, fb->vcs.scr_ri.ri_height,
287 1.5 bouyer fb->vcs.scr_ri.ri_depth);
288 1.5 bouyer
289 1.5 bouyer aprint_debug_dev(self, ": fb 0x%" PRIxBUSSIZE "@0x%" PRIxBUSADDR
290 1.5 bouyer ", mmio 0x%" PRIxBUSSIZE "@0x%" PRIxBUSADDR
291 1.5 bouyer ", io 0x%" PRIxBUSSIZE "@0x%" PRIxBUSADDR "\n",
292 1.5 bouyer fb->fbsize, fb->fbbase,
293 1.5 bouyer fb->mmiosize, fb->mmiobase,
294 1.5 bouyer fb->iosize, fb->iobase);
295 1.1 bouyer
296 1.1 bouyer fb->wsd = (struct wsscreen_descr){
297 1.1 bouyer "default",
298 1.1 bouyer 0, 0,
299 1.1 bouyer NULL,
300 1.1 bouyer 8, 16,
301 1.1 bouyer WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
302 1.1 bouyer NULL
303 1.1 bouyer };
304 1.1 bouyer sc->sc_scrlist[0] = &fb->wsd;
305 1.1 bouyer sc->sc_wsl = (struct wsscreen_list){1, sc->sc_scrlist};
306 1.1 bouyer sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
307 1.1 bouyer
308 1.1 bouyer
309 1.1 bouyer vcons_init(&sc->vd, sc, &fb->wsd, &sisfb_accessops);
310 1.1 bouyer sc->vd.init_screen = sisfb_init_screen;
311 1.1 bouyer
312 1.1 bouyer ri = &fb->vcs.scr_ri;
313 1.1 bouyer if (console) {
314 1.1 bouyer vcons_init_screen(&sc->vd, &fb->vcs, 1, &defattr);
315 1.1 bouyer fb->vcs.scr_flags |= VCONS_SCREEN_IS_STATIC;
316 1.1 bouyer fb->wsd.textops = &ri->ri_ops;
317 1.1 bouyer fb->wsd.capabilities = ri->ri_caps;
318 1.1 bouyer fb->wsd.nrows = ri->ri_rows;
319 1.1 bouyer fb->wsd.ncols = ri->ri_cols;
320 1.1 bouyer wsdisplay_cnattach(&fb->wsd, ri, 0, 0, defattr);
321 1.1 bouyer vcons_replay_msgbuf(&fb->vcs);
322 1.1 bouyer } else {
323 1.1 bouyer /*
324 1.1 bouyer * since we're not the console we can postpone the rest
325 1.1 bouyer * until someone actually allocates a screen for us
326 1.1 bouyer */
327 1.1 bouyer (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
328 1.1 bouyer }
329 1.1 bouyer
330 1.1 bouyer waa.console = console;
331 1.1 bouyer waa.scrdata = &sc->sc_wsl;
332 1.1 bouyer waa.accessops = &sisfb_accessops;
333 1.1 bouyer waa.accesscookie = &sc->vd;
334 1.1 bouyer
335 1.1 bouyer config_found(sc->sc_dev, &waa, wsemuldisplaydevprint);
336 1.1 bouyer return;
337 1.1 bouyer
338 1.1 bouyer fail3:
339 1.5 bouyer bus_space_unmap(fb->iot, fb->ioh, fb->iosize);
340 1.1 bouyer fail2:
341 1.5 bouyer bus_space_unmap(fb->mmiot, fb->mmioh, fb->mmiosize);
342 1.1 bouyer fail1:
343 1.4 bouyer bus_space_unmap(fb->fbt, fb->fbh, fb->fbsize);
344 1.1 bouyer }
345 1.1 bouyer
346 1.1 bouyer /*
347 1.1 bouyer * wsdisplay accesops
348 1.1 bouyer */
349 1.1 bouyer
350 1.1 bouyer int
351 1.1 bouyer sisfb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
352 1.1 bouyer int *curxp, int *curyp, long *attrp)
353 1.1 bouyer {
354 1.1 bouyer struct sisfb_softc *sc = (struct sisfb_softc *)v;
355 1.1 bouyer struct rasops_info *ri = &sc->sc_fb->vcs.scr_ri;
356 1.1 bouyer
357 1.1 bouyer if (sc->sc_nscr > 0)
358 1.1 bouyer return ENOMEM;
359 1.1 bouyer
360 1.1 bouyer *cookiep = ri;
361 1.1 bouyer *curxp = *curyp = 0;
362 1.1 bouyer ri->ri_ops.allocattr(ri, 0, 0, 0, attrp);
363 1.1 bouyer sc->sc_nscr++;
364 1.1 bouyer
365 1.1 bouyer return 0;
366 1.1 bouyer }
367 1.1 bouyer
368 1.1 bouyer void
369 1.1 bouyer sisfb_free_screen(void *v, void *cookie)
370 1.1 bouyer {
371 1.1 bouyer struct sisfb_softc *sc = (struct sisfb_softc *)v;
372 1.1 bouyer
373 1.1 bouyer sc->sc_nscr--;
374 1.1 bouyer }
375 1.1 bouyer
376 1.1 bouyer int
377 1.1 bouyer sisfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flags,
378 1.1 bouyer struct lwp *l)
379 1.1 bouyer {
380 1.1 bouyer struct vcons_data *vd = v;
381 1.1 bouyer struct sisfb_softc *sc = vd->cookie;
382 1.1 bouyer struct sisfb *fb = sc->sc_fb;
383 1.1 bouyer struct rasops_info *ri = &fb->vcs.scr_ri;
384 1.1 bouyer struct wsdisplay_cmap *cm;
385 1.1 bouyer struct wsdisplay_fbinfo *wdf;
386 1.1 bouyer int rc;
387 1.1 bouyer
388 1.1 bouyer switch (cmd) {
389 1.1 bouyer case WSDISPLAYIO_GTYPE:
390 1.1 bouyer *(uint *)data = WSDISPLAY_TYPE_PCIMISC;
391 1.1 bouyer return 0;
392 1.1 bouyer case WSDISPLAYIO_GINFO:
393 1.4 bouyer if (vd->active != NULL) {
394 1.4 bouyer wdf = (struct wsdisplay_fbinfo *)data;
395 1.4 bouyer wdf->width = ri->ri_width;
396 1.4 bouyer wdf->height = ri->ri_height;
397 1.4 bouyer wdf->depth = ri->ri_depth;
398 1.4 bouyer wdf->cmsize = 256;
399 1.4 bouyer return 0;
400 1.4 bouyer } else
401 1.4 bouyer return ENODEV;
402 1.1 bouyer case WSDISPLAYIO_LINEBYTES:
403 1.1 bouyer *(uint *)data = ri->ri_stride;
404 1.4 bouyer return 0;
405 1.1 bouyer case WSDISPLAYIO_GETCMAP:
406 1.1 bouyer cm = (struct wsdisplay_cmap *)data;
407 1.1 bouyer rc = sisfb_getcmap(fb->cmap, cm);
408 1.1 bouyer return rc;
409 1.1 bouyer case WSDISPLAYIO_PUTCMAP:
410 1.1 bouyer cm = (struct wsdisplay_cmap *)data;
411 1.1 bouyer rc = sisfb_putcmap(fb->cmap, cm);
412 1.1 bouyer if (rc != 0)
413 1.1 bouyer return rc;
414 1.1 bouyer if (ri->ri_depth == 8)
415 1.1 bouyer sisfb_loadcmap(fb, cm->index, cm->count);
416 1.1 bouyer return 0;
417 1.4 bouyer case WSDISPLAYIO_SMODE:
418 1.5 bouyer sc->sc_mode = *(uint *)data;
419 1.5 bouyer aprint_debug_dev(sc->sc_dev, ": switching to ");
420 1.5 bouyer switch(sc->sc_mode) {
421 1.5 bouyer case WSDISPLAYIO_MODE_EMUL:
422 1.5 bouyer aprint_debug("WSDISPLAYIO_MODE_EMUL\n");
423 1.5 bouyer break;
424 1.5 bouyer case WSDISPLAYIO_MODE_MAPPED:
425 1.5 bouyer aprint_debug("WSDISPLAYIO_MODE_MAPPED\n");
426 1.5 bouyer break;
427 1.5 bouyer case WSDISPLAYIO_MODE_DUMBFB:
428 1.5 bouyer aprint_debug("WSDISPLAYIO_MODE_DUMBFB\n");
429 1.5 bouyer break;
430 1.5 bouyer default:
431 1.5 bouyer aprint_debug("unknown mode %d\n", sc->sc_mode);
432 1.5 bouyer return EINVAL;
433 1.5 bouyer }
434 1.5 bouyer return 0;
435 1.5 bouyer case WSDISPLAYIO_GMODE:
436 1.5 bouyer *(uint *)data = sc->sc_mode;
437 1.5 bouyer return 0;
438 1.4 bouyer case PCI_IOC_CFGREAD:
439 1.4 bouyer case PCI_IOC_CFGWRITE:
440 1.4 bouyer return pci_devioctl(sc->sc_pc, sc->sc_pt, cmd, data, flags, l);
441 1.4 bouyer case WSDISPLAYIO_GET_BUSID:
442 1.4 bouyer return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
443 1.4 bouyer sc->sc_pt, data);
444 1.1 bouyer }
445 1.1 bouyer return EPASSTHROUGH;
446 1.1 bouyer }
447 1.1 bouyer
448 1.1 bouyer int
449 1.1 bouyer sisfb_show_screen(void *v, void *cookie, int waitok,
450 1.1 bouyer void (*cb)(void *, int, int), void *cbarg)
451 1.1 bouyer {
452 1.1 bouyer return 0;
453 1.1 bouyer }
454 1.1 bouyer
455 1.1 bouyer paddr_t
456 1.1 bouyer sisfb_mmap(void *v, void *vs, off_t offset, int prot)
457 1.1 bouyer {
458 1.1 bouyer struct vcons_data *vd = v;
459 1.1 bouyer struct sisfb_softc *sc = vd->cookie;
460 1.1 bouyer struct rasops_info *ri = &sc->sc_fb->vcs.scr_ri;
461 1.4 bouyer struct sisfb *fb = sc->sc_fb;
462 1.4 bouyer const uintptr_t fb_offset =
463 1.4 bouyer (uintptr_t)bus_space_vaddr(fb->fbt, fb->fbh) - (uintptr_t)fb->fb_addr;
464 1.4 bouyer paddr_t pa;
465 1.4 bouyer
466 1.5 bouyer if (sc->sc_mode != WSDISPLAYIO_MODE_MAPPED) {
467 1.5 bouyer if (offset >= 0 && offset < ri->ri_stride * ri->ri_height) {
468 1.5 bouyer pa = bus_space_mmap(fb->fbt,
469 1.5 bouyer fb->fbbase, fb_offset + offset,
470 1.5 bouyer prot, BUS_SPACE_MAP_LINEAR);
471 1.5 bouyer return pa;
472 1.5 bouyer }
473 1.5 bouyer return -1;
474 1.5 bouyer }
475 1.5 bouyer if (kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER,
476 1.5 bouyer NULL) != 0) {
477 1.5 bouyer return -1;
478 1.5 bouyer }
479 1.5 bouyer if (offset >= (fb->fbbase & ~PAGE_MASK) &&
480 1.5 bouyer offset <= ((fb->fbbase + fb->fbsize + PAGE_SIZE - 1) & ~PAGE_MASK)) {
481 1.5 bouyer pa = bus_space_mmap(fb->fbt, fb->fbbase, offset - fb->fbbase,
482 1.5 bouyer prot, BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE);
483 1.5 bouyer return pa;
484 1.5 bouyer }
485 1.5 bouyer if (offset >= (fb->mmiobase & ~PAGE_MASK) &&
486 1.5 bouyer offset <= ((fb->mmiobase + fb->mmiosize + PAGE_SIZE - 1) & ~PAGE_MASK)) {
487 1.5 bouyer pa = bus_space_mmap(fb->mmiot, fb->mmiobase, offset - fb->mmiobase,
488 1.5 bouyer prot, BUS_SPACE_MAP_LINEAR);
489 1.5 bouyer return pa;
490 1.5 bouyer }
491 1.5 bouyer if (offset >= (fb->iobase & ~PAGE_MASK) &&
492 1.5 bouyer offset <= ((fb->iobase + fb->iosize + PAGE_SIZE - 1) & ~PAGE_MASK)) {
493 1.5 bouyer pa = bus_space_mmap(fb->iot, fb->iobase, offset - fb->iobase,
494 1.5 bouyer prot, BUS_SPACE_MAP_LINEAR);
495 1.4 bouyer return pa;
496 1.4 bouyer }
497 1.4 bouyer return -1;
498 1.1 bouyer }
499 1.1 bouyer
500 1.1 bouyer void
501 1.1 bouyer sisfb_init_screen(void *cookie, struct vcons_screen *scr,
502 1.1 bouyer int existing, long *defattr)
503 1.1 bouyer {
504 1.1 bouyer struct sisfb_softc *sc = cookie;
505 1.1 bouyer struct sisfb *fb = sc->sc_fb;
506 1.1 bouyer struct rasops_info *ri = &scr->scr_ri;
507 1.1 bouyer
508 1.1 bouyer ri->ri_depth = fb->fb_depth;
509 1.1 bouyer ri->ri_width = fb->fb_width;
510 1.1 bouyer ri->ri_height = fb->fb_height;
511 1.1 bouyer ri->ri_stride = fb->fb_stride;
512 1.1 bouyer ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
513 1.1 bouyer
514 1.1 bouyer ri->ri_bits = fb->fb_addr;
515 1.1 bouyer
516 1.1 bouyer if (existing) {
517 1.1 bouyer ri->ri_flg |= RI_CLEAR;
518 1.1 bouyer }
519 1.1 bouyer
520 1.1 bouyer rasops_init(ri, fb->fb_height / 8, fb->fb_width / 8);
521 1.1 bouyer ri->ri_caps = WSSCREEN_WSCOLORS;
522 1.1 bouyer rasops_reconfig(ri, fb->fb_height / ri->ri_font->fontheight,
523 1.1 bouyer fb->fb_width / ri->ri_font->fontwidth);
524 1.1 bouyer
525 1.1 bouyer ri->ri_hw = scr;
526 1.1 bouyer }
527 1.1 bouyer
528 1.1 bouyer
529 1.1 bouyer /*
530 1.1 bouyer * Frame buffer initialization.
531 1.1 bouyer */
532 1.1 bouyer
533 1.1 bouyer int
534 1.1 bouyer sisfb_setup(struct sisfb *fb)
535 1.1 bouyer {
536 1.1 bouyer struct rasops_info *ri = &fb->vcs.scr_ri;
537 1.1 bouyer uint width, height, bpp;
538 1.1 bouyer bus_size_t fbaddr;
539 1.1 bouyer uint tmp;
540 1.1 bouyer
541 1.1 bouyer /*
542 1.1 bouyer * Unlock access to extended registers.
543 1.1 bouyer */
544 1.1 bouyer
545 1.1 bouyer sisfb_seq_write(fb, 0x05, 0x86);
546 1.1 bouyer
547 1.1 bouyer /*
548 1.1 bouyer * Try and figure out display settings.
549 1.1 bouyer */
550 1.1 bouyer
551 1.1 bouyer height = sisfb_crtc_read(fb, CRTC_VDE);
552 1.1 bouyer tmp = sisfb_crtc_read(fb, CRTC_OVERFLL);
553 1.1 bouyer if (ISSET(tmp, 1 << 1))
554 1.1 bouyer height |= 1 << 8;
555 1.1 bouyer if (ISSET(tmp, 1 << 6))
556 1.1 bouyer height |= 1 << 9;
557 1.1 bouyer tmp = sisfb_seq_read(fb, 0x0a);
558 1.1 bouyer if (ISSET(tmp, 1 << 1))
559 1.1 bouyer height |= 1 << 10;
560 1.1 bouyer height++;
561 1.1 bouyer
562 1.1 bouyer width = sisfb_crtc_read(fb, CRTC_HDISPLE);
563 1.1 bouyer tmp = sisfb_seq_read(fb, 0x0b);
564 1.1 bouyer if (ISSET(tmp, 1 << 2))
565 1.1 bouyer width |= 1 << 8;
566 1.1 bouyer if (ISSET(tmp, 1 << 3))
567 1.1 bouyer width |= 1 << 9;
568 1.1 bouyer width++;
569 1.1 bouyer width <<= 3;
570 1.1 bouyer
571 1.1 bouyer #ifdef SIS_DEBUG
572 1.1 bouyer printf("height %d width %d\n", height, width);
573 1.1 bouyer #endif
574 1.1 bouyer
575 1.1 bouyer fbaddr = sisfb_crtc_read(fb, CRTC_STARTADRL) |
576 1.1 bouyer (sisfb_crtc_read(fb, CRTC_STARTADRH) << 8) |
577 1.1 bouyer (sisfb_seq_read(fb, 0x0d) << 16) |
578 1.1 bouyer ((sisfb_seq_read(fb, 0x37) & 0x03) << 24);
579 1.1 bouyer fbaddr <<= 2;
580 1.1 bouyer #ifdef SIS_DEBUG
581 1.1 bouyer printf("FBADDR %lx\n", fbaddr);
582 1.1 bouyer #endif
583 1.1 bouyer
584 1.1 bouyer tmp = sisfb_seq_read(fb, 0x06);
585 1.1 bouyer switch (tmp & 0x1c) {
586 1.1 bouyer case 0x00:
587 1.1 bouyer bpp = 8;
588 1.1 bouyer break;
589 1.1 bouyer case 0x04:
590 1.1 bouyer bpp = 15;
591 1.1 bouyer break;
592 1.1 bouyer case 0x08:
593 1.1 bouyer bpp = 16;
594 1.1 bouyer break;
595 1.1 bouyer case 0x10:
596 1.1 bouyer bpp = 32;
597 1.1 bouyer break;
598 1.1 bouyer default:
599 1.1 bouyer aprint_error("unknown bpp for 0x%x\n", tmp);
600 1.1 bouyer return EINVAL;
601 1.1 bouyer }
602 1.1 bouyer #ifdef SIS_DEBUG
603 1.1 bouyer printf("BPP %d\n", bpp);
604 1.1 bouyer #endif
605 1.1 bouyer
606 1.1 bouyer fb->fb_width = ri->ri_width = width;
607 1.1 bouyer fb->fb_height = ri->ri_height = height;
608 1.1 bouyer fb->fb_depth = ri->ri_depth = bpp;
609 1.1 bouyer fb->fb_stride = ri->ri_stride = (ri->ri_width * ri->ri_depth) / 8;
610 1.1 bouyer ri->ri_flg = /* RI_CENTER | RI_CLEAR | RI_FULLCLEAR */ RI_CENTER | RI_NO_AUTO;
611 1.1 bouyer fb->fb_addr = ri->ri_bits =
612 1.1 bouyer (void *)((char *)bus_space_vaddr(fb->fbt, fb->fbh) + fbaddr);
613 1.1 bouyer ri->ri_hw = fb;
614 1.1 bouyer
615 1.1 bouyer #ifdef SIS_DEBUG
616 1.1 bouyer printf("ri_bits %p\n", ri->ri_bits);
617 1.1 bouyer #endif
618 1.1 bouyer
619 1.1 bouyer #ifdef __MIPSEL__
620 1.1 bouyer /* swap B and R */
621 1.1 bouyer switch (bpp) {
622 1.1 bouyer case 15:
623 1.1 bouyer ri->ri_rnum = 5;
624 1.1 bouyer ri->ri_rpos = 10;
625 1.1 bouyer ri->ri_gnum = 5;
626 1.1 bouyer ri->ri_gpos = 5;
627 1.1 bouyer ri->ri_bnum = 5;
628 1.1 bouyer ri->ri_bpos = 0;
629 1.1 bouyer break;
630 1.1 bouyer case 16:
631 1.1 bouyer ri->ri_rnum = 5;
632 1.1 bouyer ri->ri_rpos = 11;
633 1.1 bouyer ri->ri_gnum = 6;
634 1.1 bouyer ri->ri_gpos = 5;
635 1.1 bouyer ri->ri_bnum = 5;
636 1.1 bouyer ri->ri_bpos = 0;
637 1.1 bouyer break;
638 1.1 bouyer }
639 1.1 bouyer #endif
640 1.1 bouyer
641 1.1 bouyer bcopy(rasops_cmap, fb->cmap, sizeof(fb->cmap));
642 1.1 bouyer if (bpp == 8) {
643 1.1 bouyer sisfb_loadcmap(fb, 0, 256);
644 1.1 bouyer }
645 1.1 bouyer
646 1.3 bouyer rasops_init(ri, 25, 80);
647 1.1 bouyer rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
648 1.1 bouyer ri->ri_width / ri->ri_font->fontwidth);
649 1.1 bouyer
650 1.1 bouyer fb->wsd.name = "std";
651 1.1 bouyer fb->wsd.ncols = ri->ri_cols;
652 1.1 bouyer fb->wsd.nrows = ri->ri_rows;
653 1.1 bouyer fb->wsd.textops = &ri->ri_ops;
654 1.1 bouyer fb->wsd.fontwidth = ri->ri_font->fontwidth;
655 1.1 bouyer fb->wsd.fontheight = ri->ri_font->fontheight;
656 1.1 bouyer fb->wsd.capabilities = ri->ri_caps;
657 1.1 bouyer
658 1.1 bouyer return 0;
659 1.1 bouyer }
660 1.1 bouyer
661 1.1 bouyer /*
662 1.1 bouyer * Colormap handling routines.
663 1.1 bouyer */
664 1.1 bouyer
665 1.1 bouyer void
666 1.1 bouyer sisfb_loadcmap(struct sisfb *fb, int baseidx, int count)
667 1.1 bouyer {
668 1.1 bouyer uint8_t *cmap = fb->cmap + baseidx * 3;
669 1.1 bouyer
670 1.1 bouyer bus_space_write_1(fb->iot, fb->ioh, DAC_ADDR, baseidx);
671 1.1 bouyer while (count-- != 0) {
672 1.1 bouyer bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2);
673 1.1 bouyer bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2);
674 1.1 bouyer bus_space_write_1(fb->iot, fb->ioh, DAC_DATA, *cmap++ >> 2);
675 1.1 bouyer }
676 1.1 bouyer }
677 1.1 bouyer
678 1.1 bouyer int
679 1.1 bouyer sisfb_getcmap(uint8_t *cmap, struct wsdisplay_cmap *cm)
680 1.1 bouyer {
681 1.1 bouyer uint index = cm->index, count = cm->count, i;
682 1.1 bouyer uint8_t ramp[256], *dst, *src;
683 1.1 bouyer int rc;
684 1.1 bouyer
685 1.1 bouyer if (index >= 256 || count > 256 - index)
686 1.1 bouyer return EINVAL;
687 1.1 bouyer
688 1.1 bouyer index *= 3;
689 1.1 bouyer
690 1.1 bouyer src = cmap + index;
691 1.1 bouyer dst = ramp;
692 1.1 bouyer for (i = 0; i < count; i++)
693 1.1 bouyer *dst++ = *src, src += 3;
694 1.1 bouyer rc = copyout(ramp, cm->red, count);
695 1.1 bouyer if (rc != 0)
696 1.1 bouyer return rc;
697 1.1 bouyer
698 1.1 bouyer src = cmap + index + 1;
699 1.1 bouyer dst = ramp;
700 1.1 bouyer for (i = 0; i < count; i++)
701 1.1 bouyer *dst++ = *src, src += 3;
702 1.1 bouyer rc = copyout(ramp, cm->green, count);
703 1.1 bouyer if (rc != 0)
704 1.1 bouyer return rc;
705 1.1 bouyer
706 1.1 bouyer src = cmap + index + 2;
707 1.1 bouyer dst = ramp;
708 1.1 bouyer for (i = 0; i < count; i++)
709 1.1 bouyer *dst++ = *src, src += 3;
710 1.1 bouyer rc = copyout(ramp, cm->blue, count);
711 1.1 bouyer if (rc != 0)
712 1.1 bouyer return rc;
713 1.1 bouyer
714 1.1 bouyer return 0;
715 1.1 bouyer }
716 1.1 bouyer
717 1.1 bouyer int
718 1.1 bouyer sisfb_putcmap(uint8_t *cmap, struct wsdisplay_cmap *cm)
719 1.1 bouyer {
720 1.1 bouyer uint index = cm->index, count = cm->count, i;
721 1.1 bouyer uint8_t ramp[256], *dst, *src;
722 1.1 bouyer int rc;
723 1.1 bouyer
724 1.1 bouyer if (index >= 256 || count > 256 - index)
725 1.1 bouyer return EINVAL;
726 1.1 bouyer
727 1.1 bouyer index *= 3;
728 1.1 bouyer
729 1.1 bouyer rc = copyin(cm->red, ramp, count);
730 1.1 bouyer if (rc != 0)
731 1.1 bouyer return rc;
732 1.1 bouyer dst = cmap + index;
733 1.1 bouyer src = ramp;
734 1.1 bouyer for (i = 0; i < count; i++)
735 1.1 bouyer *dst = *src++, dst += 3;
736 1.1 bouyer
737 1.1 bouyer rc = copyin(cm->green, ramp, count);
738 1.1 bouyer if (rc != 0)
739 1.1 bouyer return rc;
740 1.1 bouyer dst = cmap + index + 1;
741 1.1 bouyer src = ramp;
742 1.1 bouyer for (i = 0; i < count; i++)
743 1.1 bouyer *dst = *src++, dst += 3;
744 1.1 bouyer
745 1.1 bouyer rc = copyin(cm->blue, ramp, count);
746 1.1 bouyer if (rc != 0)
747 1.1 bouyer return rc;
748 1.1 bouyer dst = cmap + index + 2;
749 1.1 bouyer src = ramp;
750 1.1 bouyer for (i = 0; i < count; i++)
751 1.1 bouyer *dst = *src++, dst += 3;
752 1.1 bouyer
753 1.1 bouyer return 0;
754 1.1 bouyer }
755 1.1 bouyer
756 1.1 bouyer /*
757 1.1 bouyer * Early console code
758 1.1 bouyer */
759 1.1 bouyer
760 1.1 bouyer int
761 1.1 bouyer sisfb_cnattach(bus_space_tag_t memt, bus_space_tag_t iot,
762 1.1 bouyer pci_chipset_tag_t pc, pcitag_t tag, pcireg_t id)
763 1.1 bouyer {
764 1.1 bouyer long defattr;
765 1.1 bouyer struct rasops_info * const ri = &sisfbcn.vcs.scr_ri;
766 1.5 bouyer int flags;
767 1.1 bouyer int rc;
768 1.1 bouyer
769 1.1 bouyer /* filter out unrecognized devices */
770 1.1 bouyer switch (id) {
771 1.1 bouyer default:
772 1.1 bouyer return ENODEV;
773 1.1 bouyer case PCI_ID_CODE(PCI_VENDOR_SIS, PCI_PRODUCT_SIS_315PRO_VGA):
774 1.1 bouyer break;
775 1.1 bouyer }
776 1.1 bouyer
777 1.5 bouyer if (pci_mapreg_info(pc, tag, PCI_MAPREG_START,
778 1.5 bouyer PCI_MAPREG_TYPE_MEM,
779 1.5 bouyer &sisfbcn.fbbase, &sisfbcn.fbsize, &flags)) {
780 1.5 bouyer printf("sisfb can't map frame buffer\n");
781 1.5 bouyer return ENODEV;
782 1.5 bouyer }
783 1.5 bouyer
784 1.1 bouyer sisfbcn.fbt = memt;
785 1.5 bouyer rc = bus_space_map(memt, sisfbcn.fbbase, sisfbcn.fbsize,
786 1.1 bouyer BUS_SPACE_MAP_LINEAR, &sisfbcn.fbh);
787 1.1 bouyer #ifdef SIS_DEBUG
788 1.1 bouyer printf("sisfb_cnattach(memt, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc);
789 1.1 bouyer #endif
790 1.1 bouyer if (rc != 0)
791 1.1 bouyer return rc;
792 1.1 bouyer
793 1.5 bouyer if (pci_mapreg_info(pc, tag, PCI_MAPREG_START + 4,
794 1.5 bouyer PCI_MAPREG_TYPE_MEM,
795 1.5 bouyer &sisfbcn.mmiobase, &sisfbcn.mmiosize, &flags)) {
796 1.5 bouyer printf("sisfb can't map mem space\n");
797 1.5 bouyer return ENODEV;
798 1.5 bouyer }
799 1.1 bouyer sisfbcn.mmiot = memt;
800 1.5 bouyer rc = bus_space_map(memt, sisfbcn.mmiobase, sisfbcn.mmiosize,
801 1.1 bouyer BUS_SPACE_MAP_LINEAR, &sisfbcn.mmioh);
802 1.1 bouyer #ifdef SIS_DEBUG
803 1.1 bouyer printf("sisfb_cnattach(memt2, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc);
804 1.1 bouyer #endif
805 1.1 bouyer if (rc != 0)
806 1.1 bouyer return rc;
807 1.1 bouyer
808 1.5 bouyer if (pci_mapreg_info(pc, tag, PCI_MAPREG_START + 8,
809 1.5 bouyer PCI_MAPREG_TYPE_IO,
810 1.5 bouyer &sisfbcn.iobase, &sisfbcn.iosize, &flags)) {
811 1.5 bouyer printf("sisfb can't map mem space\n");
812 1.5 bouyer return ENODEV;
813 1.5 bouyer }
814 1.1 bouyer sisfbcn.iot = iot;
815 1.5 bouyer rc = bus_space_map(iot, sisfbcn.iobase, sisfbcn.iosize,
816 1.1 bouyer 0, &sisfbcn.ioh);
817 1.1 bouyer #ifdef SIS_DEBUG
818 1.1 bouyer printf("sisfb_cnattach(iot, 0x%x rc %d\n", PCI_MAPREG_MEM_ADDR(bar), rc);
819 1.1 bouyer #endif
820 1.1 bouyer if (rc != 0)
821 1.1 bouyer return rc;
822 1.1 bouyer
823 1.1 bouyer rc = sisfb_setup(&sisfbcn);
824 1.1 bouyer #ifdef SIS_DEBUG
825 1.1 bouyer printf("sisfb_setup %d %p\n", rc, sisfbcn.vcs.scr_ri.ri_hw);
826 1.1 bouyer #endif
827 1.1 bouyer if (rc != 0)
828 1.1 bouyer return rc;
829 1.1 bouyer
830 1.1 bouyer ri->ri_ops.allocattr(ri, 0, ri->ri_rows - 1, 0, &defattr);
831 1.1 bouyer wsdisplay_preattach(&sisfbcn.wsd, ri, 0, 0, defattr);
832 1.1 bouyer
833 1.1 bouyer return 0;
834 1.1 bouyer }
835