mq200.c revision 1.2.2.2 1 /* $NetBSD: mq200.c,v 1.2.2.2 2000/11/20 20:46:04 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Takemura Shin
5 * 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. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36 #include <sys/reboot.h>
37
38 #include <uvm/uvm_extern.h>
39
40 #include <dev/wscons/wsconsio.h>
41
42 #include <machine/bootinfo.h>
43 #include <machine/bus.h>
44 #include <machine/autoconf.h>
45 #include <machine/config_hook.h>
46 #include <machine/platid.h>
47 #include <machine/platid_mask.h>
48
49 #include <hpcmips/dev/mq200reg.h>
50 #include <hpcmips/dev/mq200var.h>
51 #include "bivideo.h"
52 #if NBIVIDEO > 0
53 #include <hpcmips/dev/bivideovar.h>
54 #endif
55
56 #define MQ200DEBUG
57 #ifdef MQ200DEBUG
58 #ifndef MQ200DEBUG_CONF
59 #define MQ200DEBUG_CONF 0
60 #endif
61 int mq200_debug = MQ200DEBUG_CONF;
62 #define DPRINTF(arg) do { if (mq200_debug) printf arg; } while(0);
63 #define DPRINTFN(n, arg) do { if (mq200_debug > (n)) printf arg; } while (0);
64 #define VPRINTF(arg) do { if (bootverbose || mq200_debug) printf arg; } while(0);
65 #define VPRINTFN(n, arg) do { if (bootverbose || mq200_debug > (n)) printf arg; } while (0);
66 #else
67 #define DPRINTF(arg) do { } while (0);
68 #define DPRINTFN(n, arg) do { } while (0);
69 #define VPRINTF(arg) do { if (bootverbose) printf arg; } while(0);
70 #define VPRINTFN(n, arg) do { if (bootverbose) printf arg; } while (0);
71 #endif
72
73 /*
74 * function prototypes
75 */
76 static void mq200_power __P((int, void *));
77 static int mq200_hardpower __P((void *, int, long, void *));
78 static int mq200_fbinit __P((struct hpcfb_fbconf *));
79 static int mq200_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
80 static paddr_t mq200_mmap __P((void *, off_t offset, int));
81
82 /*
83 * static variables
84 */
85 struct hpcfb_accessops mq200_ha = {
86 mq200_ioctl, mq200_mmap
87 };
88
89 int
90 mq200_probe(iot, ioh)
91 bus_space_tag_t iot;
92 bus_space_handle_t ioh;
93 {
94 unsigned long regval;
95
96 #if NBIVIDEO > 0
97 if (bivideo_dont_attach) /* some video driver already attached */
98 return (0);
99 #endif /* NBIVIDEO > 0 */
100
101 regval = bus_space_read_4(iot, ioh, MQ200_PC00R);
102 VPRINTF(("mq200 probe: vendor id=%04lx product id=%04lx\n",
103 regval & 0xffff, (regval >> 16) & 0xffff));
104 if (regval != ((MQ200_PRODUCT_ID << 16) | MQ200_VENDOR_ID))
105 return (0);
106
107 return (1);
108 }
109
110 void
111 mq200_attach(sc)
112 struct mq200_softc *sc;
113 {
114 unsigned long regval;
115 struct hpcfb_attach_args ha;
116 int console = (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) ? 0 : 1;
117
118 regval = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MQ200_PC08R);
119 printf(": MQ200 Rev.%02lx video controller\n", regval & 0xff);
120
121 /* Add a power hook to power saving */
122 sc->sc_powerstate = MQ200_POWERSTATE_D0;
123 sc->sc_powerhook = powerhook_establish(mq200_power, sc);
124 if (sc->sc_powerhook == NULL)
125 printf("%s: WARNING: unable to establish power hook\n",
126 sc->sc_dev.dv_xname);
127
128 /* Add a hard power hook to power saving */
129 sc->sc_hardpowerhook = config_hook(CONFIG_HOOK_PMEVENT,
130 CONFIG_HOOK_PMEVENT_HARDPOWER,
131 CONFIG_HOOK_SHARE,
132 mq200_hardpower, sc);
133 if (sc->sc_hardpowerhook == NULL)
134 printf("%s: WARNING: unable to establish hard power hook\n",
135 sc->sc_dev.dv_xname);
136
137 mq200_fbinit(&sc->sc_fbconf);
138
139 if (console && hpcfb_cnattach(&sc->sc_fbconf) != 0) {
140 panic("mq200_attach: can't init fb console");
141 }
142
143 ha.ha_console = console;
144 ha.ha_accessops = &mq200_ha;
145 ha.ha_accessctx = sc;
146 ha.ha_curfbconf = 0;
147 ha.ha_nfbconf = 1;
148 ha.ha_fbconflist = &sc->sc_fbconf;
149 ha.ha_curdspconf = 0;
150 ha.ha_ndspconf = 1;
151 ha.ha_dspconflist = &sc->sc_dspconf;
152
153 config_found(&sc->sc_dev, &ha, hpcfbprint);
154
155 #if NBIVIDEO > 0
156 /*
157 * bivideo is no longer need
158 */
159 bivideo_dont_attach = 1;
160 #endif /* NBIVIDEO > 0 */
161 }
162
163 static void
164 mq200_power(why, arg)
165 int why;
166 void *arg;
167 {
168 #if 0
169 struct mq200_softc *sc = arg;
170
171 switch (why) {
172 case PWR_SUSPEND:
173 sc->sc_powerstate = MQ200_POWERSTATE_D2;
174 break;
175 case PWR_STANDBY:
176 sc->sc_powerstate = MQ200_POWERSTATE_D3;
177 break;
178 case PWR_RESUME:
179 sc->sc_powerstate = MQ200_POWERSTATE_D0;
180 break;
181 }
182
183 printf("MQ200_PMCSR=%08x\n", sc->sc_powerstate);
184 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
185 MQ200_PMCSR, sc->sc_powerstate);
186 #endif
187 }
188
189 static int
190 mq200_hardpower(ctx, type, id, msg)
191 void *ctx;
192 int type;
193 long id;
194 void *msg;
195 {
196 struct mq200_softc *sc = ctx;
197 int why = (int)msg;
198
199 switch (why) {
200 case PWR_SUSPEND:
201 sc->sc_powerstate = MQ200_POWERSTATE_D2;
202 break;
203 case PWR_STANDBY:
204 sc->sc_powerstate = MQ200_POWERSTATE_D3;
205 break;
206 case PWR_RESUME:
207 sc->sc_powerstate = MQ200_POWERSTATE_D0;
208 break;
209 }
210
211 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
212 MQ200_PMCSR, sc->sc_powerstate);
213
214 /*
215 * you should wait until the
216 * power state transit sequence will end.
217 */
218 {
219 unsigned long tmp;
220 do {
221 tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
222 MQ200_PMCSR);
223 } while ((tmp & 0x3) != (sc->sc_powerstate & 0x3));
224 delay(100000); /* XXX */
225 }
226
227 return (0);
228 }
229
230
231 static int
232 mq200_fbinit(fb)
233 struct hpcfb_fbconf *fb;
234 {
235
236 /*
237 * get fb settings from bootinfo
238 */
239 if (bootinfo == NULL ||
240 bootinfo->fb_addr == 0 ||
241 bootinfo->fb_line_bytes == 0 ||
242 bootinfo->fb_width == 0 ||
243 bootinfo->fb_height == 0) {
244 printf("no frame buffer infomation.\n");
245 return (-1);
246 }
247
248 /* zero fill */
249 bzero(fb, sizeof(*fb));
250
251 fb->hf_conf_index = 0; /* configuration index */
252 fb->hf_nconfs = 1; /* how many configurations */
253 strcpy(fb->hf_name, "built-in video");
254 /* frame buffer name */
255 strcpy(fb->hf_conf_name, "default");
256 /* configuration name */
257 fb->hf_height = bootinfo->fb_height;
258 fb->hf_width = bootinfo->fb_width;
259 fb->hf_baseaddr = mips_ptob(mips_btop(bootinfo->fb_addr));
260 fb->hf_offset = (u_long)bootinfo->fb_addr - fb->hf_baseaddr;
261 /* frame buffer start offset */
262 fb->hf_bytes_per_line = bootinfo->fb_line_bytes;
263 fb->hf_nplanes = 1;
264 fb->hf_bytes_per_plane = bootinfo->fb_height *
265 bootinfo->fb_line_bytes;
266
267 fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
268 fb->hf_access_flags |= HPCFB_ACCESS_WORD;
269 fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
270
271 switch (bootinfo->fb_type) {
272 /*
273 * gray scale
274 */
275 case BIFB_D2_M2L_3:
276 case BIFB_D2_M2L_3x2:
277 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
278 /* fall through */
279 case BIFB_D2_M2L_0:
280 case BIFB_D2_M2L_0x2:
281 fb->hf_class = HPCFB_CLASS_GRAYSCALE;
282 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
283 fb->hf_pack_width = 8;
284 fb->hf_pixels_per_pack = 4;
285 fb->hf_pixel_width = 2;
286 fb->hf_class_data_length = sizeof(struct hf_gray_tag);
287 fb->hf_u.hf_gray.hf_flags = 0; /* reserved for future use */
288 break;
289
290 /*
291 * indexed color
292 */
293 case BIFB_D8_FF:
294 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
295 /* fall through */
296 case BIFB_D8_00:
297 fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
298 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
299 fb->hf_pack_width = 8;
300 fb->hf_pixels_per_pack = 1;
301 fb->hf_pixel_width = 8;
302 fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
303 fb->hf_u.hf_indexed.hf_flags = 0; /* reserved for future use */
304 break;
305
306 /*
307 * RGB color
308 */
309 case BIFB_D16_FFFF:
310 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
311 /* fall through */
312 case BIFB_D16_0000:
313 fb->hf_class = HPCFB_CLASS_RGBCOLOR;
314 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
315 #if BYTE_ORDER == LITTLE_ENDIAN
316 fb->hf_swap_flags = HPCFB_SWAP_BYTE;
317 #endif
318 fb->hf_pack_width = 16;
319 fb->hf_pixels_per_pack = 1;
320 fb->hf_pixel_width = 16;
321
322 fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
323 fb->hf_u.hf_rgb.hf_flags = 0; /* reserved for future use */
324
325 fb->hf_u.hf_rgb.hf_red_width = 5;
326 fb->hf_u.hf_rgb.hf_red_shift = 11;
327 fb->hf_u.hf_rgb.hf_green_width = 6;
328 fb->hf_u.hf_rgb.hf_green_shift = 5;
329 fb->hf_u.hf_rgb.hf_blue_width = 5;
330 fb->hf_u.hf_rgb.hf_blue_shift = 0;
331 fb->hf_u.hf_rgb.hf_alpha_width = 0;
332 fb->hf_u.hf_rgb.hf_alpha_shift = 0;
333 break;
334
335 default:
336 printf("unknown type (=%d).\n", bootinfo->fb_type);
337 return (-1);
338 break;
339 }
340
341 return (0); /* no error */
342 }
343
344 int
345 mq200_ioctl(v, cmd, data, flag, p)
346 void *v;
347 u_long cmd;
348 caddr_t data;
349 int flag;
350 struct proc *p;
351 {
352 struct mq200_softc *sc = (struct mq200_softc *)v;
353 struct hpcfb_fbconf *fbconf;
354 struct hpcfb_dspconf *dspconf;
355 struct wsdisplay_cmap *cmap;
356
357 switch (cmd) {
358 case WSDISPLAYIO_GETCMAP:
359 cmap = (struct wsdisplay_cmap*)data;
360
361 if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
362 sc->sc_fbconf.hf_pack_width != 8 ||
363 256 <= cmap->index ||
364 256 < (cmap->index + cmap->count))
365 return (EINVAL);
366
367 #if 0
368 if (!uvm_useracc(cmap->red, cmap->count, B_WRITE) ||
369 !uvm_useracc(cmap->green, cmap->count, B_WRITE) ||
370 !uvm_useracc(cmap->blue, cmap->count, B_WRITE))
371 return (EFAULT);
372
373 copyout(&bivideo_cmap_r[cmap->index], cmap->red, cmap->count);
374 copyout(&bivideo_cmap_g[cmap->index], cmap->green,cmap->count);
375 copyout(&bivideo_cmap_b[cmap->index], cmap->blue, cmap->count);
376 #endif
377
378 return (0);
379
380 case WSDISPLAYIO_PUTCMAP:
381 /*
382 * This driver can't set color map.
383 */
384 return (EINVAL);
385
386 case HPCFBIO_GCONF:
387 fbconf = (struct hpcfb_fbconf *)data;
388 if (fbconf->hf_conf_index != 0 &&
389 fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
390 return (EINVAL);
391 }
392 *fbconf = sc->sc_fbconf; /* structure assignment */
393 return (0);
394 case HPCFBIO_SCONF:
395 fbconf = (struct hpcfb_fbconf *)data;
396 if (fbconf->hf_conf_index != 0 &&
397 fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
398 return (EINVAL);
399 }
400 /*
401 * nothing to do because we have only one configration
402 */
403 return (0);
404 case HPCFBIO_GDSPCONF:
405 dspconf = (struct hpcfb_dspconf *)data;
406 if ((dspconf->hd_unit_index != 0 &&
407 dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
408 (dspconf->hd_conf_index != 0 &&
409 dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
410 return (EINVAL);
411 }
412 *dspconf = sc->sc_dspconf; /* structure assignment */
413 return (0);
414 case HPCFBIO_SDSPCONF:
415 dspconf = (struct hpcfb_dspconf *)data;
416 if ((dspconf->hd_unit_index != 0 &&
417 dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
418 (dspconf->hd_conf_index != 0 &&
419 dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
420 return (EINVAL);
421 }
422 /*
423 * nothing to do
424 * because we have only one unit and one configration
425 */
426 return (0);
427 case HPCFBIO_GOP:
428 case HPCFBIO_SOP:
429 /*
430 * curently not implemented...
431 */
432 return (EINVAL);
433 }
434
435 return (ENOTTY);
436 }
437
438 paddr_t
439 mq200_mmap(ctx, offset, prot)
440 void *ctx;
441 off_t offset;
442 int prot;
443 {
444 struct mq200_softc *sc = (struct mq200_softc *)ctx;
445
446 if (offset < 0 ||
447 (sc->sc_fbconf.hf_bytes_per_plane +
448 sc->sc_fbconf.hf_offset) < offset)
449 return -1;
450
451 return mips_btop(sc->sc_fbconf.hf_baseaddr + offset);
452 }
453