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