xafb.c revision 1.16.8.1 1 /* $NetBSD: xafb.c,v 1.16.8.1 2014/05/22 11:40:00 yamt 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.16.8.1 2014/05/22 11:40:00 yamt 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 ri->ri_flg &= ~RI_NO_AUTO;
156 sc->sc_nscreens = 1;
157 } else {
158 dc = malloc(sizeof(struct xafb_devconfig), M_DEVBUF,
159 M_WAITOK|M_ZERO);
160 dc->dc_fbpaddr = (paddr_t)0x10000000;
161 dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
162 dc->dc_reg = (void *)(apa->apa_hwbase + 0x3000);
163 if (xafb_common_init(dc) != 0) {
164 aprint_error(": couldn't initialize device\n");
165 return;
166 }
167
168 ri = &dc->dc_ri;
169
170 /* clear screen */
171 (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, 0);
172 }
173 sc->sc_dc = dc;
174
175 for (i = 0; i < 256; i++) {
176 sc->sc_cmap_red[i] = i;
177 sc->sc_cmap_green[i] = i;
178 sc->sc_cmap_blue[i] = i;
179 }
180
181 aprint_normal(": %d x %d, %dbpp\n",
182 ri->ri_width, ri->ri_height, ri->ri_depth);
183
184 wsa.console = console;
185 wsa.scrdata = &xafb_screenlist;
186 wsa.accessops = &xafb_accessops;
187 wsa.accesscookie = sc;
188
189 config_found(self, &wsa, wsemuldisplaydevprint);
190 }
191
192 void
193 xafb_setcolor(struct xafb_devconfig *dc, int index, int r, int g, int b)
194 {
195 volatile struct xafb_reg *reg = dc->dc_reg;
196
197 reg->index = index;
198 reg->zero = 0;
199 reg->rgb = r;
200 reg->rgb = g;
201 reg->rgb = b;
202 }
203
204 int
205 xafb_common_init(struct xafb_devconfig *dc)
206 {
207 struct rasops_info *ri = &dc->dc_ri;
208 int i;
209
210 for (i = 0; i < 256; i++)
211 xafb_setcolor(dc, i, i, i, i);
212
213 /* initialize rasops */
214 ri->ri_width = 1280;
215 ri->ri_height = 1024;
216 ri->ri_depth = 8;
217 ri->ri_stride = 2048;
218 ri->ri_bits = (void *)dc->dc_fbbase;
219 ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
220 if (dc == &xafb_console_dc)
221 ri->ri_flg |= RI_NO_AUTO;
222
223 rasops_init(ri, 44, 100);
224
225 /* mono */
226 ri->ri_devcmap[0] = 0; /* bg */
227 ri->ri_devcmap[1] = 0xffffffff; /* fg */
228
229 xafb_stdscreen.nrows = ri->ri_rows;
230 xafb_stdscreen.ncols = ri->ri_cols;
231 xafb_stdscreen.textops = &ri->ri_ops;
232 xafb_stdscreen.capabilities = ri->ri_caps;
233
234 return 0;
235 }
236
237 int
238 xafb_is_console(void)
239 {
240 volatile uint32_t *dipsw = (void *)NEWS5000_DIP_SWITCH;
241
242 if (*dipsw & 1) /* XXX right? */
243 return 1;
244
245 return 0;
246 }
247
248 int
249 xafb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
250 {
251 struct xafb_softc *sc = v;
252 struct xafb_devconfig *dc = sc->sc_dc;
253 struct newsmips_wsdisplay_fbinfo *nwdf = (void *)data;
254 struct wsdisplay_fbinfo *wdf = (void *)data;
255
256 switch (cmd) {
257 case WSDISPLAYIO_GTYPE:
258 *(int *)data = WSDISPLAY_TYPE_UNKNOWN; /* XXX */
259 return 0;
260
261 case NEWSMIPS_WSDISPLAYIO_GINFO:
262 nwdf->stride = dc->dc_ri.ri_stride;
263 /* FALLTHROUGH */
264 case WSDISPLAYIO_GINFO:
265 wdf->height = dc->dc_ri.ri_height;
266 wdf->width = dc->dc_ri.ri_width;
267 wdf->depth = dc->dc_ri.ri_depth;
268 wdf->cmsize = 256;
269 return 0;
270
271 case WSDISPLAYIO_LINEBYTES:
272 *(u_int *)data = dc->dc_ri.ri_stride;
273 return 0;
274
275 case WSDISPLAYIO_GETCMAP:
276 return xafb_getcmap(sc, (struct wsdisplay_cmap *)data);
277
278 case WSDISPLAYIO_PUTCMAP:
279 return xafb_putcmap(sc, (struct wsdisplay_cmap *)data);
280
281 /* case WSDISPLAYIO_SVIDEO: */
282 }
283 return EPASSTHROUGH;
284 }
285
286 paddr_t
287 xafb_mmap(void *v, void *vs, off_t offset, int prot)
288 {
289 struct xafb_softc *sc = v;
290 struct xafb_devconfig *dc = sc->sc_dc;
291 struct rasops_info *ri = &dc->dc_ri;
292
293 if (offset >= (ri->ri_stride * ri->ri_height) || offset < 0)
294 return -1;
295
296 return mips_btop(dc->dc_fbpaddr + offset);
297 }
298
299 int
300 xafb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc,
301 void **cookiep, int *ccolp, int *crowp, long *attrp)
302 {
303 struct xafb_softc *sc = v;
304 struct rasops_info *ri = &sc->sc_dc->dc_ri;
305 long defattr;
306
307 if (sc->sc_nscreens > 0)
308 return ENOMEM;
309
310 *cookiep = ri;
311 *ccolp = *crowp = 0;
312 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
313 *attrp = defattr;
314 sc->sc_nscreens++;
315
316 return 0;
317 }
318
319 void
320 xafb_free_screen(void *v, void *cookie)
321 {
322 struct xafb_softc *sc = v;
323
324 if (sc->sc_dc == &xafb_console_dc)
325 panic("xafb_free_screen: console");
326
327 sc->sc_nscreens--;
328 }
329
330 int
331 xafb_show_screen(void *v, void *cookie, int waitok,
332 void (*cb)(void *, int, int), void *cbarg)
333 {
334
335 return 0;
336 }
337
338 int
339 xafb_cnattach(void)
340 {
341 struct xafb_devconfig *dc = &xafb_console_dc;
342 struct rasops_info *ri = &dc->dc_ri;
343 long defattr;
344 int crow = 0;
345
346 if (!xafb_is_console())
347 return -1;
348
349 dc->dc_fbpaddr = (paddr_t)0x10000000;
350 dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
351 dc->dc_reg = (void *)0xb4903000; /* XXX */
352 xafb_common_init(dc);
353
354 crow = 0; /* XXX current cursor pos */
355
356 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
357 wsdisplay_cnattach(&xafb_stdscreen, &dc->dc_ri, 0, crow, defattr);
358
359 return 0;
360 }
361
362 int
363 xafb_getcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
364 {
365 u_int index = cm->index;
366 u_int count = cm->count;
367 int error;
368
369 if (index >= 256 || count > 256 || index + count > 256)
370 return EINVAL;
371
372 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
373 if (error)
374 return error;
375 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
376 if (error)
377 return error;
378 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
379 if (error)
380 return error;
381
382 return 0;
383 }
384
385 int
386 xafb_putcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
387 {
388 u_int index = cm->index;
389 u_int count = cm->count;
390 int i, error;
391 u_char rbuf[256], gbuf[256], bbuf[256];
392 u_char *r, *g, *b;
393
394 if (cm->index >= 256 || cm->count > 256 ||
395 (cm->index + cm->count) > 256)
396 return EINVAL;
397 error = copyin(cm->red, &rbuf[index], count);
398 if (error)
399 return error;
400 error = copyin(cm->green, &gbuf[index], count);
401 if (error)
402 return error;
403 error = copyin(cm->blue, &bbuf[index], count);
404 if (error)
405 return error;
406
407 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
408 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
409 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
410
411 r = &sc->sc_cmap_red[index];
412 g = &sc->sc_cmap_green[index];
413 b = &sc->sc_cmap_blue[index];
414 for (i = 0; i < count; i++)
415 xafb_setcolor(sc->sc_dc, index++, *r++, *g++, *b++);
416 return 0;
417 }
418