bivideo.c revision 1.2 1 /* $NetBSD: bivideo.c,v 1.2 2001/02/24 19:51:20 uch Exp $ */
2
3 /*-
4 * Copyright (c) 1999-2001
5 * Shin Takemura and PocketBSD Project. All rights reserved.
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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the PocketBSD project
18 * and its contributors.
19 * 4. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36 #define FBDEBUG
37 static const char _copyright[] __attribute__ ((unused)) =
38 "Copyright (c) 1999 Shin Takemura. All rights reserved.";
39 static const char _rcsid[] __attribute__ ((unused)) =
40 "$Id: bivideo.c,v 1.2 2001/02/24 19:51:20 uch Exp $";
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/buf.h>
46 #include <sys/ioctl.h>
47 #include <sys/reboot.h>
48
49 #include <uvm/uvm_extern.h>
50
51 #include <machine/bus.h>
52 #include <machine/autoconf.h>
53 #include <machine/bootinfo.h>
54 #include <machine/config_hook.h>
55
56 #include <dev/wscons/wsconsio.h>
57 #include <dev/wscons/wsdisplayvar.h>
58
59 #include <dev/rasops/rasops.h>
60
61 #include <dev/hpc/hpcfbvar.h>
62 #include <dev/hpc/hpcfbio.h>
63 #include <dev/hpc/bivideovar.h>
64 #include <dev/hpc/hpccmapvar.h>
65
66 #define VPRINTF(arg) do { if (bootverbose) printf arg; } while(0);
67
68 /*
69 * global variables
70 */
71 int bivideo_dont_attach = 0;
72
73 /*
74 * function prototypes
75 */
76 int bivideomatch(struct device *, struct cfdata *, void *);
77 void bivideoattach(struct device *, struct device *, void *);
78 int bivideo_ioctl(void *, u_long, caddr_t, int, struct proc *);
79 paddr_t bivideo_mmap(void *, off_t, int);
80
81 struct bivideo_softc {
82 struct device sc_dev;
83 struct hpcfb_fbconf sc_fbconf;
84 struct hpcfb_dspconf sc_dspconf;
85 void *sc_powerhook; /* power management hook */
86 int sc_powerstate;
87 #define PWRSTAT_SUSPEND (1<<0)
88 #define PWRSTAT_LCD (1<<1)
89 #define PWRSTAT_BACKLIGHT (1<<2)
90 #define PWRSTAT_ALL (0xffffffff)
91 };
92 static int bivideo_init(struct hpcfb_fbconf *);
93 static void bivideo_power(int, void *);
94 static void bivideo_update_powerstate(struct bivideo_softc *, int);
95
96 #if defined __mips__ || defined __sh__ || defined __arm__
97 #define __BTOP(x) ((paddr_t)(x) >> PGSHIFT)
98 #define __PTOB(x) ((paddr_t)(x) << PGSHIFT)
99 #else
100 #error "define btop, ptob."
101 #endif
102
103 /*
104 * static variables
105 */
106 struct cfattach bivideo_ca = {
107 sizeof(struct bivideo_softc), bivideomatch, bivideoattach,
108 };
109 struct hpcfb_accessops bivideo_ha = {
110 bivideo_ioctl, bivideo_mmap
111 };
112
113 static int console_flag = 0;
114 static int attach_flag = 0;
115
116 /*
117 * function bodies
118 */
119 int
120 bivideomatch(struct device *parent, struct cfdata *match, void *aux)
121 {
122 struct mainbus_attach_args *ma = aux;
123
124 if (bivideo_dont_attach ||
125 strcmp(ma->ma_name, match->cf_driver->cd_name))
126 return 0;
127
128 return (1);
129 }
130
131 void
132 bivideoattach(struct device *parent, struct device *self, void *aux)
133 {
134 struct bivideo_softc *sc = (struct bivideo_softc *)self;
135 struct hpcfb_attach_args ha;
136
137 if (attach_flag) {
138 panic("%s(%d): bivideo attached twice", __FILE__, __LINE__);
139 }
140 attach_flag = 1;
141
142 printf(": ");
143 if (bivideo_init(&sc->sc_fbconf) != 0) {
144 /* just return so that hpcfb will not be attached */
145 return;
146 }
147
148 printf("pseudo video controller");
149 if (console_flag) {
150 printf(", console");
151 }
152 printf("\n");
153 printf("%s: framebuffer address: 0x%08lx\n",
154 sc->sc_dev.dv_xname, (u_long)bootinfo->fb_addr);
155
156 /* Add a suspend hook to power saving */
157 sc->sc_powerstate = 0;
158 sc->sc_powerhook = powerhook_establish(bivideo_power, sc);
159 if (sc->sc_powerhook == NULL)
160 printf("%s: WARNING: unable to establish power hook\n",
161 sc->sc_dev.dv_xname);
162
163 ha.ha_console = console_flag;
164 ha.ha_accessops = &bivideo_ha;
165 ha.ha_accessctx = sc;
166 ha.ha_curfbconf = 0;
167 ha.ha_nfbconf = 1;
168 ha.ha_fbconflist = &sc->sc_fbconf;
169 ha.ha_curdspconf = 0;
170 ha.ha_ndspconf = 1;
171 ha.ha_dspconflist = &sc->sc_dspconf;
172
173 config_found(self, &ha, hpcfbprint);
174 }
175
176 int
177 bivideo_getcnfb(struct hpcfb_fbconf *fb)
178 {
179 console_flag = 1;
180
181 return bivideo_init(fb);
182 }
183
184 static int
185 bivideo_init(struct hpcfb_fbconf *fb)
186 {
187 /*
188 * get fb settings from bootinfo
189 */
190 if (bootinfo == NULL ||
191 bootinfo->fb_addr == 0 ||
192 bootinfo->fb_line_bytes == 0 ||
193 bootinfo->fb_width == 0 ||
194 bootinfo->fb_height == 0) {
195 printf("no frame buffer infomation.\n");
196 return (-1);
197 }
198
199 /* zero fill */
200 bzero(fb, sizeof(*fb));
201
202 fb->hf_conf_index = 0; /* configuration index */
203 fb->hf_nconfs = 1; /* how many configurations */
204 strcpy(fb->hf_name, "built-in video");
205 /* frame buffer name */
206 strcpy(fb->hf_conf_name, "default");
207 /* configuration name */
208 fb->hf_height = bootinfo->fb_height;
209 fb->hf_width = bootinfo->fb_width;
210 fb->hf_baseaddr = (u_long)bootinfo->fb_addr;
211 fb->hf_offset = (u_long)bootinfo->fb_addr -
212 __PTOB(__BTOP(bootinfo->fb_addr));
213 /* frame buffer start offset */
214 fb->hf_bytes_per_line = bootinfo->fb_line_bytes;
215 fb->hf_nplanes = 1;
216 fb->hf_bytes_per_plane = bootinfo->fb_height *
217 bootinfo->fb_line_bytes;
218
219 fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
220 fb->hf_access_flags |= HPCFB_ACCESS_WORD;
221 fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
222
223 switch (bootinfo->fb_type) {
224 /*
225 * gray scale
226 */
227 case BIFB_D2_M2L_3:
228 case BIFB_D2_M2L_3x2:
229 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
230 /* fall through */
231 case BIFB_D2_M2L_0:
232 case BIFB_D2_M2L_0x2:
233 fb->hf_class = HPCFB_CLASS_GRAYSCALE;
234 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
235 fb->hf_pack_width = 8;
236 fb->hf_pixels_per_pack = 4;
237 fb->hf_pixel_width = 2;
238 fb->hf_class_data_length = sizeof(struct hf_gray_tag);
239 fb->hf_u.hf_gray.hf_flags = 0; /* reserved for future use */
240 break;
241
242 case BIFB_D4_M2L_F:
243 case BIFB_D4_M2L_Fx2:
244 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
245 /* fall through */
246 case BIFB_D4_M2L_0:
247 case BIFB_D4_M2L_0x2:
248 fb->hf_class = HPCFB_CLASS_GRAYSCALE;
249 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
250 fb->hf_pack_width = 8;
251 fb->hf_pixels_per_pack = 2;
252 fb->hf_pixel_width = 4;
253 fb->hf_class_data_length = sizeof(struct hf_gray_tag);
254 fb->hf_u.hf_gray.hf_flags = 0; /* reserved for future use */
255 break;
256
257 /*
258 * indexed color
259 */
260 case BIFB_D8_FF:
261 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
262 /* fall through */
263 case BIFB_D8_00:
264 fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
265 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
266 fb->hf_pack_width = 8;
267 fb->hf_pixels_per_pack = 1;
268 fb->hf_pixel_width = 8;
269 fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
270 fb->hf_u.hf_indexed.hf_flags = 0; /* reserved for future use */
271 break;
272
273 /*
274 * RGB color
275 */
276 case BIFB_D16_FFFF:
277 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
278 /* fall through */
279 case BIFB_D16_0000:
280 fb->hf_class = HPCFB_CLASS_RGBCOLOR;
281 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
282 #if BYTE_ORDER == LITTLE_ENDIAN
283 fb->hf_swap_flags = HPCFB_SWAP_BYTE;
284 #endif
285 fb->hf_pack_width = 16;
286 fb->hf_pixels_per_pack = 1;
287 fb->hf_pixel_width = 16;
288
289 fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
290 fb->hf_u.hf_rgb.hf_flags = 0; /* reserved for future use */
291
292 fb->hf_u.hf_rgb.hf_red_width = 5;
293 fb->hf_u.hf_rgb.hf_red_shift = 11;
294 fb->hf_u.hf_rgb.hf_green_width = 6;
295 fb->hf_u.hf_rgb.hf_green_shift = 5;
296 fb->hf_u.hf_rgb.hf_blue_width = 5;
297 fb->hf_u.hf_rgb.hf_blue_shift = 0;
298 fb->hf_u.hf_rgb.hf_alpha_width = 0;
299 fb->hf_u.hf_rgb.hf_alpha_shift = 0;
300 break;
301
302 default:
303 printf("unsupported type %d.\n", bootinfo->fb_type);
304 return (-1);
305 break;
306 }
307
308 return (0); /* no error */
309 }
310
311 static void
312 bivideo_power(int why, void *arg)
313 {
314 struct bivideo_softc *sc = arg;
315
316 switch (why) {
317 case PWR_SUSPEND:
318 case PWR_STANDBY:
319 sc->sc_powerstate |= PWRSTAT_SUSPEND;
320 bivideo_update_powerstate(sc, PWRSTAT_ALL);
321 break;
322 case PWR_RESUME:
323 sc->sc_powerstate &= ~PWRSTAT_SUSPEND;
324 bivideo_update_powerstate(sc, PWRSTAT_ALL);
325 break;
326 }
327 }
328
329 static void
330 bivideo_update_powerstate(struct bivideo_softc *sc, int updates)
331 {
332 if (updates & PWRSTAT_LCD)
333 config_hook_call(CONFIG_HOOK_POWERCONTROL,
334 CONFIG_HOOK_POWERCONTROL_LCD,
335 (void*)!(sc->sc_powerstate & PWRSTAT_SUSPEND));
336
337 if (updates & PWRSTAT_BACKLIGHT)
338 config_hook_call(CONFIG_HOOK_POWERCONTROL,
339 CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
340 (void*)(!(sc->sc_powerstate & PWRSTAT_SUSPEND) &&
341 (sc->sc_powerstate & PWRSTAT_BACKLIGHT)));
342 }
343
344 int
345 bivideo_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
346 {
347 struct bivideo_softc *sc = (struct bivideo_softc *)v;
348 struct hpcfb_fbconf *fbconf;
349 struct hpcfb_dspconf *dspconf;
350 struct wsdisplay_cmap *cmap;
351 struct wsdisplay_param *dispparam;
352
353 switch (cmd) {
354 case WSDISPLAYIO_GETCMAP:
355 cmap = (struct wsdisplay_cmap*)data;
356
357 if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
358 sc->sc_fbconf.hf_pack_width != 8 ||
359 256 <= cmap->index ||
360 256 < (cmap->index + cmap->count))
361 return (EINVAL);
362
363 if (!uvm_useracc(cmap->red, cmap->count, B_WRITE) ||
364 !uvm_useracc(cmap->green, cmap->count, B_WRITE) ||
365 !uvm_useracc(cmap->blue, cmap->count, B_WRITE))
366 return (EFAULT);
367
368 copyout(&bivideo_cmap_r[cmap->index], cmap->red, cmap->count);
369 copyout(&bivideo_cmap_g[cmap->index], cmap->green,cmap->count);
370 copyout(&bivideo_cmap_b[cmap->index], cmap->blue, cmap->count);
371
372 return (0);
373
374 case WSDISPLAYIO_PUTCMAP:
375 /*
376 * This driver can't set color map.
377 */
378 return (EINVAL);
379
380 case WSDISPLAYIO_GETPARAM:
381 dispparam = (struct wsdisplay_param*)data;
382 switch (dispparam->param) {
383 case WSDISPLAYIO_PARAM_BACKLIGHT:
384 dispparam->min = 0;
385 dispparam->max = 1;
386 dispparam->curval =
387 (sc->sc_powerstate & PWRSTAT_BACKLIGHT) ? 1 : 0;
388 VPRINTF(("bivideo_ioctl: GETPARAM:BACKLIGHT:%d\n",
389 dispparam->curval));
390 break;
391 case WSDISPLAYIO_PARAM_CONTRAST:
392 VPRINTF(("bivideo_ioctl: GETPARAM:CONTRAST\n"));
393 return (EINVAL);
394 case WSDISPLAYIO_PARAM_BRIGHTNESS:
395 VPRINTF(("bivideo_ioctl: GETPARAM:BRIGHTNESS\n"));
396 return (EINVAL);
397 default:
398 return (EINVAL);
399 }
400 return (0);
401
402 case WSDISPLAYIO_SETPARAM:
403 dispparam = (struct wsdisplay_param*)data;
404 switch (dispparam->param) {
405 case WSDISPLAYIO_PARAM_BACKLIGHT:
406 if (dispparam->curval < 0 ||
407 1 < dispparam->curval)
408 return (EINVAL);
409 if (dispparam->curval == 0)
410 sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
411 else
412 sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
413 bivideo_update_powerstate(sc, PWRSTAT_BACKLIGHT);
414 VPRINTF(("bivideo_ioctl: SETPARAM:BACKLIGHT:%d\n",
415 (sc->sc_powerstate & PWRSTAT_BACKLIGHT)?1:0));
416 break;
417 case WSDISPLAYIO_PARAM_CONTRAST:
418 VPRINTF(("bivideo_ioctl: SETPARAM:CONTRAST\n"));
419 return (EINVAL);
420 case WSDISPLAYIO_PARAM_BRIGHTNESS:
421 VPRINTF(("mq200_ioctl: SETPARAM:BRIGHTNESS\n"));
422 return (EINVAL);
423 default:
424 return (EINVAL);
425 }
426 return (0);
427
428 case HPCFBIO_GCONF:
429 fbconf = (struct hpcfb_fbconf *)data;
430 if (fbconf->hf_conf_index != 0 &&
431 fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
432 return (EINVAL);
433 }
434 *fbconf = sc->sc_fbconf; /* structure assignment */
435 return (0);
436 case HPCFBIO_SCONF:
437 fbconf = (struct hpcfb_fbconf *)data;
438 if (fbconf->hf_conf_index != 0 &&
439 fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
440 return (EINVAL);
441 }
442 /*
443 * nothing to do because we have only one configration
444 */
445 return (0);
446 case HPCFBIO_GDSPCONF:
447 dspconf = (struct hpcfb_dspconf *)data;
448 if ((dspconf->hd_unit_index != 0 &&
449 dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
450 (dspconf->hd_conf_index != 0 &&
451 dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
452 return (EINVAL);
453 }
454 *dspconf = sc->sc_dspconf; /* structure assignment */
455 return (0);
456 case HPCFBIO_SDSPCONF:
457 dspconf = (struct hpcfb_dspconf *)data;
458 if ((dspconf->hd_unit_index != 0 &&
459 dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
460 (dspconf->hd_conf_index != 0 &&
461 dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
462 return (EINVAL);
463 }
464 /*
465 * nothing to do
466 * because we have only one unit and one configration
467 */
468 return (0);
469 case HPCFBIO_GOP:
470 case HPCFBIO_SOP:
471 /*
472 * curently not implemented...
473 */
474 return (EINVAL);
475 }
476
477 return (ENOTTY);
478 }
479
480 paddr_t
481 bivideo_mmap(void *ctx, off_t offset, int prot)
482 {
483 struct bivideo_softc *sc = (struct bivideo_softc *)ctx;
484
485 if (offset < 0 ||
486 (sc->sc_fbconf.hf_bytes_per_plane +
487 sc->sc_fbconf.hf_offset) < offset)
488 return -1;
489
490 return __BTOP((u_long)bootinfo->fb_addr + offset);
491 }
492