xafb.c revision 1.15 1 /* $NetBSD: xafb.c,v 1.15 2008/04/09 15:40:30 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* "xa" frame buffer driver. Currently supports 1280x1024x8 only. */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: xafb.c,v 1.15 2008/04/09 15:40:30 tsutsui Exp $");
33
34 #include <sys/param.h>
35 #include <sys/buf.h>
36 #include <sys/device.h>
37 #include <sys/ioctl.h>
38 #include <sys/malloc.h>
39 #include <sys/systm.h>
40
41 #include <uvm/uvm_extern.h>
42
43 #include <machine/adrsmap.h>
44 #include <machine/apcall.h>
45
46 #include <machine/wsconsio.h>
47 #include <dev/wscons/wsdisplayvar.h>
48 #include <dev/rasops/rasops.h>
49
50 #include <newsmips/apbus/apbusvar.h>
51
52 struct xafb_reg {
53 volatile uint32_t r0;
54 volatile uint32_t index;
55 volatile uint32_t r2;
56 volatile uint32_t zero;
57 volatile uint32_t r4;
58 volatile uint32_t r5;
59 volatile uint32_t r6;
60 volatile uint32_t rgb;
61 };
62
63 struct xafb_devconfig {
64 uint8_t *dc_fbbase; /* VRAM base address */
65 paddr_t dc_fbpaddr; /* VRAM physical address */
66 struct xafb_reg *dc_reg; /* register address */
67 struct rasops_info dc_ri;
68 };
69
70 struct xafb_softc {
71 device_t sc_dev;
72 struct xafb_devconfig *sc_dc;
73 int sc_nscreens;
74 uint8_t sc_cmap_red[256];
75 uint8_t sc_cmap_green[256];
76 uint8_t sc_cmap_blue[256];
77 };
78
79 int xafb_match(device_t, cfdata_t, void *);
80 void xafb_attach(device_t, device_t, void *);
81
82 int xafb_common_init(struct xafb_devconfig *);
83 int xafb_is_console(void);
84
85 int xafb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
86 paddr_t xafb_mmap(void *, void *, off_t, int);
87 int xafb_alloc_screen(void *, const struct wsscreen_descr *, void **, int *,
88 int *, long *);
89 void xafb_free_screen(void *, void *);
90 int xafb_show_screen(void *, void *, int, void (*) (void *, int, int), void *);
91
92 int xafb_cnattach(void);
93 int xafb_getcmap(struct xafb_softc *, struct wsdisplay_cmap *);
94 int xafb_putcmap(struct xafb_softc *, struct wsdisplay_cmap *);
95
96 static inline void xafb_setcolor(struct xafb_devconfig *, int, int, int, int);
97
98 CFATTACH_DECL_NEW(xafb, sizeof(struct xafb_softc),
99 xafb_match, xafb_attach, NULL, NULL);
100
101 struct xafb_devconfig xafb_console_dc;
102
103 struct wsdisplay_accessops xafb_accessops = {
104 xafb_ioctl,
105 xafb_mmap,
106 xafb_alloc_screen,
107 xafb_free_screen,
108 xafb_show_screen,
109 NULL /* load_font */
110 };
111
112 struct wsscreen_descr xafb_stdscreen = {
113 "std",
114 0, 0,
115 0,
116 0, 0,
117 WSSCREEN_REVERSE
118 };
119
120 const struct wsscreen_descr *xafb_scrlist[] = {
121 &xafb_stdscreen
122 };
123
124 struct wsscreen_list xafb_screenlist = {
125 __arraycount(xafb_scrlist), xafb_scrlist
126 };
127
128 int
129 xafb_match(device_t parent, cfdata_t cf, void *aux)
130 {
131 struct apbus_attach_args *apa = aux;
132
133 if (strcmp(apa->apa_name, "xa") != 0)
134 return 0;
135
136 return 1;
137 }
138
139 void
140 xafb_attach(device_t parent, device_t self, void *aux)
141 {
142 struct xafb_softc *sc = device_private(self);
143 struct apbus_attach_args *apa = aux;
144 struct wsemuldisplaydev_attach_args wsa;
145 struct xafb_devconfig *dc;
146 struct rasops_info *ri;
147 int console, i;
148
149 sc->sc_dev = self;
150 console = xafb_is_console();
151
152 if (console) {
153 dc = &xafb_console_dc;
154 ri = &dc->dc_ri;
155 sc->sc_nscreens = 1;
156 } else {
157 dc = malloc(sizeof(struct xafb_devconfig), M_DEVBUF,
158 M_WAITOK|M_ZERO);
159 dc->dc_fbpaddr = (paddr_t)0x10000000;
160 dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
161 dc->dc_reg = (void *)(apa->apa_hwbase + 0x3000);
162 if (xafb_common_init(dc) != 0) {
163 aprint_error(": couldn't initialize device\n");
164 return;
165 }
166
167 ri = &dc->dc_ri;
168
169 /* clear screen */
170 (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, 0);
171 }
172 sc->sc_dc = dc;
173
174 for (i = 0; i < 256; i++) {
175 sc->sc_cmap_red[i] = i;
176 sc->sc_cmap_green[i] = i;
177 sc->sc_cmap_blue[i] = i;
178 }
179
180 aprint_normal(": %d x %d, %dbpp\n",
181 ri->ri_width, ri->ri_height, ri->ri_depth);
182
183 wsa.console = console;
184 wsa.scrdata = &xafb_screenlist;
185 wsa.accessops = &xafb_accessops;
186 wsa.accesscookie = sc;
187
188 config_found(self, &wsa, wsemuldisplaydevprint);
189 }
190
191 void
192 xafb_setcolor(struct xafb_devconfig *dc, int index, int r, int g, int b)
193 {
194 volatile struct xafb_reg *reg = dc->dc_reg;
195
196 reg->index = index;
197 reg->zero = 0;
198 reg->rgb = r;
199 reg->rgb = g;
200 reg->rgb = b;
201 }
202
203 int
204 xafb_common_init(struct xafb_devconfig *dc)
205 {
206 struct rasops_info *ri = &dc->dc_ri;
207 int i;
208
209 for (i = 0; i < 256; i++)
210 xafb_setcolor(dc, i, i, i, i);
211
212 /* initialize rasops */
213 ri->ri_width = 1280;
214 ri->ri_height = 1024;
215 ri->ri_depth = 8;
216 ri->ri_stride = 2048;
217 ri->ri_bits = (void *)dc->dc_fbbase;
218 ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
219
220 rasops_init(ri, 44, 100);
221
222 /* mono */
223 ri->ri_devcmap[0] = 0; /* bg */
224 ri->ri_devcmap[1] = 0xffffffff; /* fg */
225
226 xafb_stdscreen.nrows = ri->ri_rows;
227 xafb_stdscreen.ncols = ri->ri_cols;
228 xafb_stdscreen.textops = &ri->ri_ops;
229 xafb_stdscreen.capabilities = ri->ri_caps;
230
231 return 0;
232 }
233
234 int
235 xafb_is_console(void)
236 {
237 volatile uint32_t *dipsw = (void *)NEWS5000_DIP_SWITCH;
238
239 if (*dipsw & 1) /* XXX right? */
240 return 1;
241
242 return 0;
243 }
244
245 int
246 xafb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
247 {
248 struct xafb_softc *sc = v;
249 struct xafb_devconfig *dc = sc->sc_dc;
250 struct newsmips_wsdisplay_fbinfo *nwdf = (void *)data;
251 struct wsdisplay_fbinfo *wdf = (void *)data;
252
253 switch (cmd) {
254 case WSDISPLAYIO_GTYPE:
255 *(int *)data = WSDISPLAY_TYPE_UNKNOWN; /* XXX */
256 return 0;
257
258 case NEWSMIPS_WSDISPLAYIO_GINFO:
259 nwdf->stride = dc->dc_ri.ri_stride;
260 /* FALLTHROUGH */
261 case WSDISPLAYIO_GINFO:
262 wdf->height = dc->dc_ri.ri_height;
263 wdf->width = dc->dc_ri.ri_width;
264 wdf->depth = dc->dc_ri.ri_depth;
265 wdf->cmsize = 256;
266 return 0;
267
268 case WSDISPLAYIO_GETCMAP:
269 return xafb_getcmap(sc, (struct wsdisplay_cmap *)data);
270
271 case WSDISPLAYIO_PUTCMAP:
272 return xafb_putcmap(sc, (struct wsdisplay_cmap *)data);
273
274 /* case WSDISPLAYIO_SVIDEO: */
275 }
276 return EPASSTHROUGH;
277 }
278
279 paddr_t
280 xafb_mmap(void *v, void *vs, off_t offset, int prot)
281 {
282 struct xafb_softc *sc = v;
283 struct xafb_devconfig *dc = sc->sc_dc;
284 struct rasops_info *ri = &dc->dc_ri;
285
286 if (offset >= (ri->ri_stride * ri->ri_height) || offset < 0)
287 return -1;
288
289 return mips_btop(dc->dc_fbpaddr + offset);
290 }
291
292 int
293 xafb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc,
294 void **cookiep, int *ccolp, int *crowp, long *attrp)
295 {
296 struct xafb_softc *sc = v;
297 struct rasops_info *ri = &sc->sc_dc->dc_ri;
298 long defattr;
299
300 if (sc->sc_nscreens > 0)
301 return ENOMEM;
302
303 *cookiep = ri;
304 *ccolp = *crowp = 0;
305 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
306 *attrp = defattr;
307 sc->sc_nscreens++;
308
309 return 0;
310 }
311
312 void
313 xafb_free_screen(void *v, void *cookie)
314 {
315 struct xafb_softc *sc = v;
316
317 if (sc->sc_dc == &xafb_console_dc)
318 panic("xafb_free_screen: console");
319
320 sc->sc_nscreens--;
321 }
322
323 int
324 xafb_show_screen(void *v, void *cookie, int waitok,
325 void (*cb)(void *, int, int), void *cbarg)
326 {
327
328 return 0;
329 }
330
331 int
332 xafb_cnattach(void)
333 {
334 struct xafb_devconfig *dc = &xafb_console_dc;
335 struct rasops_info *ri = &dc->dc_ri;
336 long defattr;
337 int crow = 0;
338
339 if (!xafb_is_console())
340 return -1;
341
342 dc->dc_fbpaddr = (paddr_t)0x10000000;
343 dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
344 dc->dc_reg = (void *)0xb4903000; /* XXX */
345 xafb_common_init(dc);
346
347 crow = 0; /* XXX current cursor pos */
348
349 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
350 wsdisplay_cnattach(&xafb_stdscreen, &dc->dc_ri, 0, crow, defattr);
351
352 return 0;
353 }
354
355 int
356 xafb_getcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
357 {
358 u_int index = cm->index;
359 u_int count = cm->count;
360 int error;
361
362 if (index >= 256 || count > 256 || index + count > 256)
363 return EINVAL;
364
365 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
366 if (error)
367 return error;
368 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
369 if (error)
370 return error;
371 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
372 if (error)
373 return error;
374
375 return 0;
376 }
377
378 int
379 xafb_putcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
380 {
381 u_int index = cm->index;
382 u_int count = cm->count;
383 int i, error;
384 u_char rbuf[256], gbuf[256], bbuf[256];
385 u_char *r, *g, *b;
386
387 if (cm->index >= 256 || cm->count > 256 ||
388 (cm->index + cm->count) > 256)
389 return EINVAL;
390 error = copyin(cm->red, &rbuf[index], count);
391 if (error)
392 return error;
393 error = copyin(cm->green, &gbuf[index], count);
394 if (error)
395 return error;
396 error = copyin(cm->blue, &bbuf[index], count);
397 if (error)
398 return error;
399
400 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
401 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
402 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
403
404 r = &sc->sc_cmap_red[index];
405 g = &sc->sc_cmap_green[index];
406 b = &sc->sc_cmap_blue[index];
407 for (i = 0; i < count; i++)
408 xafb_setcolor(sc->sc_dc, index++, *r++, *g++, *b++);
409 return 0;
410 }
411