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